<!DOCTYPE html>
<html lang="zh-Hans">
<head>

    <!--[if lt IE 9]>
        <style>body {display: none; background: none !important} </style>
        <meta http-equiv="Refresh" Content="0; url=//outdatedbrowser.com/" />
    <![endif]-->

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<meta name="format-detection" content="telephone=no" />
<meta name="author" content="zhengweiMao" />



<meta name="description" content="注意不同版本的可能会存在差异 我这次解析的版本是1.5.11.RELEASE.">
<meta property="og:type" content="article">
<meta property="og:title" content="1.SpringBoot源码分析之SpringBoot的启动过程">
<meta property="og:url" content="https://maozw.gitee.io/maoge-blog/2018/08/05/花神(maoge)/9.Spring/SpringBoot/SpringBoot-启动源码解析/index.html">
<meta property="og:site_name" content="花神">
<meta property="og:description" content="注意不同版本的可能会存在差异 我这次解析的版本是1.5.11.RELEASE.">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2018-08-07T13:37:32.555Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="1.SpringBoot源码分析之SpringBoot的启动过程">
<meta name="twitter:description" content="注意不同版本的可能会存在差异 我这次解析的版本是1.5.11.RELEASE.">

<link rel="apple-touch-icon" href= "/apple-touch-icon.png">


    <link rel="alternate" href="/atom.xml" title="花神" type="application/atom+xml">



    <link rel="shortcut icon" href="/favicon.png">



    <link href="//cdn.bootcss.com/animate.css/3.5.1/animate.min.css" rel="stylesheet">



    <link href="//cdn.bootcss.com/fancybox/2.1.5/jquery.fancybox.min.css" rel="stylesheet">



    <script src="//cdn.bootcss.com/pace/1.0.2/pace.min.js"></script>
    <link href="//cdn.bootcss.com/pace/1.0.2/themes/blue/pace-theme-minimal.css" rel="stylesheet">


<link rel="stylesheet" href="/maoge-blog/css/style.css">



<link href="//cdn.bootcss.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet">


<title>1.SpringBoot源码分析之SpringBoot的启动过程 | 花神</title>

<script src="//cdn.bootcss.com/jquery/2.2.4/jquery.min.js"></script>
<script src="//cdn.bootcss.com/clipboard.js/1.5.10/clipboard.min.js"></script>

<script>
    var yiliaConfig = {
        fancybox: true,
        animate: true,
        isHome: false,
        isPost: true,
        isArchive: false,
        isTag: false,
        isCategory: false,
        fancybox_js: "//cdn.bootcss.com/fancybox/2.1.5/jquery.fancybox.min.js",
        scrollreveal: "//cdn.bootcss.com/scrollReveal.js/3.1.4/scrollreveal.min.js",
        search: true
    }
</script>


    <script>
        yiliaConfig.jquery_ui = [true, "//cdn.bootcss.com/jqueryui/1.10.4/jquery-ui.min.js", "//cdn.bootcss.com/jqueryui/1.10.4/css/jquery-ui.min.css"];
    </script>



    <script> yiliaConfig.rootUrl = "\/";</script>






</head>
<body>
  <div id="container">
    <div class="left-col">
    <div class="overlay"></div>
<div class="intrude-less">
    <header id="header" class="inner">
        <a href="/maoge-blog/" class="profilepic">
            <img src="img/avatar.png" class="animated zoomIn">
        </a>
        <hgroup>
          <h1 class="header-author"><a href="/maoge-blog/">zhengweiMao</a></h1>
        </hgroup>

        

        
            <form id="search-form">
            <input type="text" id="local-search-input" name="q" placeholder="search..." class="search form-control" autocomplete="off" autocorrect="off" searchonload="" />
            <i class="fa fa-times" onclick="resetSearch()"></i>
            </form>
            <div id="local-search-result"></div>
            <p class='no-result'>No results found <i class='fa fa-spinner fa-pulse'></i></p>
        


        
            <div id="switch-btn" class="switch-btn">
                <div class="icon">
                    <div class="icon-ctn">
                        <div class="icon-wrap icon-house" data-idx="0">
                            <div class="birdhouse"></div>
                            <div class="birdhouse_holes"></div>
                        </div>
                        <div class="icon-wrap icon-ribbon hide" data-idx="1">
                            <div class="ribbon"></div>
                        </div>
                        
                        <div class="icon-wrap icon-link hide" data-idx="2">
                            <div class="loopback_l"></div>
                            <div class="loopback_r"></div>
                        </div>
                        
                        
                        <div class="icon-wrap icon-me hide" data-idx="3">
                            <div class="user"></div>
                            <div class="shoulder"></div>
                        </div>
                        
                    </div>
                    
                </div>
                <div class="tips-box hide">
                    <div class="tips-arrow"></div>
                    <ul class="tips-inner">
                        <li>菜单</li>
                        <li>标签</li>
                        
                        <li>友情链接</li>
                        
                        
                        <li>关于我</li>
                        
                    </ul>
                </div>
            </div>
        

        <div id="switch-area" class="switch-area">
            <div class="switch-wrap">
                <section class="switch-part switch-part1">
                    <nav class="header-menu">
                        <ul>
                        
                            <li><a href="/maoge-blog/">主页</a></li>
                        
                            <li><a href="/maoge-blog/archives/">所有文章</a></li>
                        
                            <li><a href="/maoge-blog/tags/">标签云</a></li>
                        
                            <li><a href="/maoge-blog/categories/">分类</a></li>
                        
                            <li><a href="/maoge-blog/about/">关于我</a></li>
                        
                        </ul>
                    </nav>
                    <nav class="header-nav">
                        <ul class="social">
                            
                                <a class="fa Email" href="mailto:123@123.com" title="Email"></a>
                            
                                <a class="fa GitHub" href="#" title="GitHub"></a>
                            
                                <a class="fa RSS" href="/maoge-blog/atom.xml" title="RSS"></a>
                            
                        </ul>
                    </nav>
                </section>
                
                
                <section class="switch-part switch-part2">
                    <div class="widget tagcloud" id="js-tagcloud">
                        
                    </div>
                </section>
                
                
                
                <section class="switch-part switch-part3">
                    <div id="js-friends">
                    
                      <a class="main-nav-link switch-friends-link" href="https://hexo.io">Hexo</a>
                    
                      <a class="main-nav-link switch-friends-link" href="https://pages.github.com/">GitHub</a>
                    
                      <a class="main-nav-link switch-friends-link" href="http://moxfive.xyz/">MOxFIVE</a>
                    
                    </div>
                </section>
                

                
                
                <section class="switch-part switch-part4">
                
                    <div id="js-aboutme">专注于前端</div>
                </section>
                
            </div>
        </div>
    </header>                
</div>
    </div>
    <div class="mid-col">
      <nav id="mobile-nav">
      <div class="overlay">
          <div class="slider-trigger"></div>
          <h1 class="header-author js-mobile-header hide"><a href="/" title="回到主页">zhengweiMao</a></h1>
      </div>
    <div class="intrude-less">
        <header id="header" class="inner">
            <a href="/maoge-blog/" class="profilepic">
                <img src="img/avatar.png" class="animated zoomIn">
            </a>
            <hgroup>
              <h1 class="header-author"><a href="/maoge-blog/" title="回到主页">zhengweiMao</a></h1>
            </hgroup>
            
            <nav class="header-menu">
                <ul>
                
                    <li><a href="/maoge-blog/">主页</a></li>
                
                    <li><a href="/maoge-blog/archives/">所有文章</a></li>
                
                    <li><a href="/maoge-blog/tags/">标签云</a></li>
                
                    <li><a href="/maoge-blog/categories/">分类</a></li>
                
                    <li><a href="/maoge-blog/about/">关于我</a></li>
                
                <div class="clearfix"></div>
                </ul>
            </nav>
            <nav class="header-nav">
                        <ul class="social">
                            
                                <a class="fa Email" target="_blank" href="mailto:123@123.com" title="Email"></a>
                            
                                <a class="fa GitHub" target="_blank" href="#" title="GitHub"></a>
                            
                                <a class="fa RSS" target="_blank" href="/maoge-blog/atom.xml" title="RSS"></a>
                            
                        </ul>
            </nav>
        </header>                
    </div>
    <link class="menu-list" tags="标签" friends="友情链接" about="关于我"/>
</nav>
      <div class="body-wrap"><article id="post-花神(maoge)/9.Spring/SpringBoot/SpringBoot-启动源码解析" class="article article-type-post" itemscope itemprop="blogPost">
  
    <div class="article-meta">
      <a href="/maoge-blog/2018/08/05/花神(maoge)/9.Spring/SpringBoot/SpringBoot-启动源码解析/" class="article-date">
      <time datetime="2018-08-05T04:06:00.000Z" itemprop="datePublished">2018-08-05</time>
</a>


    </div>
  
  <div class="article-inner">
    
      <input type="hidden" class="isFancy" />
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      1.SpringBoot源码分析之SpringBoot的启动过程
    </h1>
  

      </header>
      
      <div class="article-info article-info-post">
        
    <div class="article-category tagcloud">
    <a class="article-category-link" href="/maoge-blog/categories/源码分析-SpringBoot/">源码分析-SpringBoot</a>
    </div>


        
        <div class="clearfix"></div>
      </div>
      
    
    <div class="article-entry" itemprop="articleBody">
      
          
        <p><excerpt in="" index="" |="" 首页摘要=""><br><a id="more"></a></excerpt></p>
<the rest="" of="" contents="" |="" 余下全文="">


<h2 id="SpringBoot源码分析之SpringBoot的启动过程"><a href="#SpringBoot源码分析之SpringBoot的启动过程" class="headerlink" title="SpringBoot源码分析之SpringBoot的启动过程"></a>SpringBoot源码分析之SpringBoot的启动过程</h2><h4 id="序言"><a href="#序言" class="headerlink" title="序言"></a>序言</h4><h2 id="注意不同版本的可能会存在差异-我这次解析的版本是1-5-11-RELEASE"><a href="#注意不同版本的可能会存在差异-我这次解析的版本是1-5-11-RELEASE" class="headerlink" title="注意不同版本的可能会存在差异 我这次解析的版本是1.5.11.RELEASE"></a>注意不同版本的可能会存在差异 我这次解析的版本是1.5.11.RELEASE</h2><h4 id="SpringBoot的启动代码"><a href="#SpringBoot的启动代码" class="headerlink" title="SpringBoot的启动代码"></a>SpringBoot的启动代码</h4><ul>
<li style="list-style: none"><input type="checkbox" checked> 很简单，我们使用SpringBoot 只需要两个步骤，仅此容易；</li>
<li><p>添加依赖</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.5.11.RELEASE<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>然后增加增加一个启动类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@SpringBootApplication</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">OnLineTaskApplication</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> Logger LOGGER = LoggerFactory.getLogger(OnLineTaskApplication.class);</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        SpringApplication.run(OnLineTaskApplication.class, args);</span><br><span class="line">        LOGGER.info(<span class="string">"Application启动！"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<hr>
<h5 id="1-OK！-那我们分析下这段代码SpringBoot究竟帮我们做了什么事情！"><a href="#1-OK！-那我们分析下这段代码SpringBoot究竟帮我们做了什么事情！" class="headerlink" title="1. OK！ 那我们分析下这段代码SpringBoot究竟帮我们做了什么事情！"></a>1. OK！ 那我们分析下这段代码SpringBoot究竟帮我们做了什么事情！</h5><p>从上述代码上可以看出，调用了SpringApplication的静态方法run,这个run方法会构造一个SpringApplication的实例，然后再调用这里实例的run方法就表示启动SpringBoot。<br>@SpringBootApplication 关于这个注解暂时不谈；<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">...<span class="comment">//省略部分代码</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Static helper that can be used to run a &#123;<span class="doctag">@link</span> SpringApplication&#125; from the</span></span><br><span class="line"><span class="comment"> * specified source using default settings.</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> source the source to load</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> args the application arguments (usually passed from a Java main method)</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> the running &#123;<span class="doctag">@link</span> ApplicationContext&#125;</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> ConfigurableApplicationContext <span class="title">run</span><span class="params">(Object source, String... args)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> run(<span class="keyword">new</span> Object[] &#123; source &#125;, args);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">...<span class="comment">//省略部分代码</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * Static helper that can be used to run a &#123;<span class="doctag">@link</span> SpringApplication&#125; from the</span></span><br><span class="line"><span class="comment">	 * specified sources using default settings and user supplied arguments.</span></span><br><span class="line"><span class="comment">	 * <span class="doctag">@param</span> sources the sources to load</span></span><br><span class="line"><span class="comment">	 * <span class="doctag">@param</span> args the application arguments (usually passed from a Java main method)</span></span><br><span class="line"><span class="comment">	 * <span class="doctag">@return</span> the running &#123;<span class="doctag">@link</span> ApplicationContext&#125;</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> ConfigurableApplicationContext <span class="title">run</span><span class="params">(Object[] sources, String[] args)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> <span class="keyword">new</span> SpringApplication(sources).run(args);</span><br><span class="line">	&#125;</span><br></pre></td></tr></table></figure></p>
<h5 id="2-然后我们关注下这个构造方法具体做了什么事情呢？"><a href="#2-然后我们关注下这个构造方法具体做了什么事情呢？" class="headerlink" title="2. 然后我们关注下这个构造方法具体做了什么事情呢？"></a>2. 然后我们关注下这个构造方法具体做了什么事情呢？</h5><p>源码如下：其实构造器调用了initialize(…)方法<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Create a new &#123;<span class="doctag">@link</span> SpringApplication&#125; instance. The application context will load</span></span><br><span class="line"><span class="comment"> * beans from the specified sources (see &#123;<span class="doctag">@link</span> SpringApplication class-level&#125;</span></span><br><span class="line"><span class="comment"> * documentation for details. The instance can be customized before calling</span></span><br><span class="line"><span class="comment"> * &#123;<span class="doctag">@link</span> #run(String...)&#125;.</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> sources the bean sources</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@see</span> #run(Object, String[])</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@see</span> #SpringApplication(ResourceLoader, Object...)</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="title">SpringApplication</span><span class="params">(Object... sources)</span> </span>&#123;</span><br><span class="line">    <span class="comment">//sources使我们启动类OnLineTaskApplication的class对象</span></span><br><span class="line">	initialize(sources);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="meta">@SuppressWarnings</span>(&#123; <span class="string">"unchecked"</span>, <span class="string">"rawtypes"</span> &#125;)</span><br><span class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">initialize</span><span class="params">(Object[] sources)</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 把sources设置到SpringApplication的sources属性中，本程序就是这个sources对象就是OnLineTaskApplication类对象 </span></span><br><span class="line">    <span class="comment">//sources = new LinkedHashSet&lt;Object&gt;();  LinkedHashSet类型</span></span><br><span class="line">	<span class="keyword">if</span> (sources != <span class="keyword">null</span> &amp;&amp; sources.length &gt; <span class="number">0</span>) &#123;</span><br><span class="line">		<span class="keyword">this</span>.sources.addAll(Arrays.asList(sources));</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">// 判断是否是web程序</span></span><br><span class="line">	<span class="keyword">this</span>.webEnvironment = deduceWebEnvironment();</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//从spring.factories文件中找出key为ApplicationContextInitializer的类并实例化后设置到SpringApplication的initializers属性中。</span></span><br><span class="line">	<span class="comment">//这个过程也就是找出所有的应用程序初始化器</span></span><br><span class="line">	setInitializers((Collection) getSpringFactoriesInstances(</span><br><span class="line">			ApplicationContextInitializer.class));</span><br><span class="line">	<span class="comment">// 从spring.factories文件中找出key为ApplicationListener的类并实例化后设置到SpringApplication的listeners属性中。这个过程就是找出所有的应用程序事件监听器</span></span><br><span class="line">	setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));</span><br><span class="line">	<span class="comment">//找出main类，这里是OnLineTaskApplication类</span></span><br><span class="line">	<span class="keyword">this</span>.mainApplicationClass = deduceMainApplicationClass();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h6 id="简要分析deduceWebEnvironment"><a href="#简要分析deduceWebEnvironment" class="headerlink" title="简要分析deduceWebEnvironment()"></a>简要分析deduceWebEnvironment()</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">boolean</span> <span class="title">deduceWebEnvironment</span><span class="params">()</span> </span>&#123;</span><br><span class="line">	<span class="keyword">for</span> (String className : WEB_ENVIRONMENT_CLASSES) &#123;</span><br><span class="line">		<span class="keyword">if</span> (!ClassUtils.isPresent(className, <span class="keyword">null</span>)) &#123;</span><br><span class="line">			<span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String[] WEB_ENVIRONMENT_CLASSES = &#123; <span class="string">"javax.servlet.Servlet"</span>,</span><br><span class="line">			<span class="string">"org.springframework.web.context.ConfigurableWebApplicationContext"</span> &#125;;</span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Determine whether the &#123;<span class="doctag">@link</span> Class&#125; identified by the supplied name is present</span></span><br><span class="line"><span class="comment"> * and can be loaded. Will return &#123;<span class="doctag">@code</span> false&#125; if either the class or</span></span><br><span class="line"><span class="comment"> * one of its dependencies is not present or cannot be loaded.</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> className the name of the class to check</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> classLoader the class loader to use</span></span><br><span class="line"><span class="comment"> * (may be &#123;<span class="doctag">@code</span> null&#125;, which indicates the default class loader)</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> whether the specified class is present</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">boolean</span> <span class="title">isPresent</span><span class="params">(String className, ClassLoader classLoader)</span> </span>&#123;</span><br><span class="line">	<span class="keyword">try</span> &#123;</span><br><span class="line">		forName(className, classLoader);</span><br><span class="line">		<span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">catch</span> (Throwable ex) &#123;</span><br><span class="line">		<span class="comment">// Class or one of its dependencies is not present...</span></span><br><span class="line">		<span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<ul>
<li>判断是否是web程序:方法里面调用了ClassUtils.isPresent方法，检查classpath中是否存在javax.servlet.Servlet类和org.springframework.web.context.ConfigurableWebApplicationContext类，并赋值给SpringApplication对象的webEnvironment属性，该属性是一个boolean类型，表示启动的程序是否是一个web应用程序</li>
</ul>
</blockquote>
<ul>
<li>简要说下 ApplicationContextInitializer, ApplicationListener<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">ApplicationContextInitializer</span>&lt;<span class="title">C</span> <span class="keyword">extends</span> <span class="title">ConfigurableApplicationContext</span>&gt; </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">initialize</span><span class="params">(C applicationContext)</span></span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<blockquote>
<ul>
<li>ApplicationContextInitializer 应用程序初始化器，做一些初始化的工作：在classpath下的JAR文件中包含的/META/spring.factories文件里找到org.springframework.context.ApplicationContextInitializer对应的属性，然后实例化并排序，设置到SpringApplication对象的initializers属性，该属性是一个ArrayList类型</li>
</ul>
</blockquote>
<blockquote>
<ul>
<li>在classpath下的JAR文件中包含的/META/spring.factories文件里找到org.springframework.context.ApplicationListener对应的属性，然后实例化并排序，设置到SpringApplication对象的listeners属性，该属性是一个ArrayList类型</li>
<li>ApplicationListener 应用程序事件(ApplicationEvent)监听器<blockquote>
<ul>
<li>应用程序事件(ApplicationEvent)有应用程序启动事件(ApplicationStartedEvent)，失败事件(ApplicationFailedEvent)，准备事件(ApplicationPreparedEvent)等<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">ApplicationListener</span>&lt;<span class="title">E</span> <span class="keyword">extends</span> <span class="title">ApplicationEvent</span>&gt; <span class="keyword">extends</span> <span class="title">EventListener</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">onApplicationEvent</span><span class="params">(E event)</span></span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ul>
</blockquote>
</li>
</ul>
</blockquote>
<h6 id="简要分析下deduceMainApplicationClass"><a href="#简要分析下deduceMainApplicationClass" class="headerlink" title="简要分析下deduceMainApplicationClass"></a>简要分析下deduceMainApplicationClass</h6><blockquote>
<ul>
<li>调用deduceMainApplicationClass方法找到main函数所在的类，实现的方式是获取当前方法调用栈，找到main函数的类，并设置到SpringApplication对象的mainApplicationClass属性，该属性是一个Class类型</li>
</ul>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> Class&lt;?&gt; deduceMainApplicationClass() &#123;</span><br><span class="line">	<span class="keyword">try</span> &#123;</span><br><span class="line">		StackTraceElement[] stackTrace = <span class="keyword">new</span> RuntimeException().getStackTrace();</span><br><span class="line">		<span class="keyword">for</span> (StackTraceElement stackTraceElement : stackTrace) &#123;</span><br><span class="line">			<span class="keyword">if</span> (<span class="string">"main"</span>.equals(stackTraceElement.getMethodName())) &#123;</span><br><span class="line">				<span class="keyword">return</span> Class.forName(stackTraceElement.getClassName());</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">catch</span> (ClassNotFoundException ex) &#123;</span><br><span class="line">		<span class="comment">// Swallow and continue</span></span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="到此为止呢-我们的SpringApplication对象初始化完成了！"><a href="#到此为止呢-我们的SpringApplication对象初始化完成了！" class="headerlink" title="到此为止呢 我们的SpringApplication对象初始化完成了！"></a>到此为止呢 我们的SpringApplication对象初始化完成了！</h5><hr>
<h4 id="3-分析完其构造器调用了SpringApplication-initialize-方法之后-我们在看看它的run-String…-args-方法"><a href="#3-分析完其构造器调用了SpringApplication-initialize-方法之后-我们在看看它的run-String…-args-方法" class="headerlink" title="3.分析完其构造器调用了SpringApplication.initialize()方法之后,我们在看看它的run(String… args)方法"></a>3.分析完其构造器调用了SpringApplication.initialize()方法之后,我们在看看它的run(String… args)方法</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Run the Spring application, creating and refreshing a new</span></span><br><span class="line"><span class="comment"> * &#123;<span class="doctag">@link</span> ApplicationContext&#125;.</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> args the application arguments (usually passed from a Java main method)</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> a running &#123;<span class="doctag">@link</span> ApplicationContext&#125;</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> ConfigurableApplicationContext <span class="title">run</span><span class="params">(String... args)</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 构造一个任务执行观察器 该类是Spring提供的一个计时器类</span></span><br><span class="line">	StopWatch stopWatch = <span class="keyword">new</span> StopWatch();</span><br><span class="line">	stopWatch.start();</span><br><span class="line">	ConfigurableApplicationContext context = <span class="keyword">null</span>;</span><br><span class="line">	FailureAnalyzers analyzers = <span class="keyword">null</span>;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//设置系统属性java.awt.headless</span></span><br><span class="line">	configureHeadlessProperty();</span><br><span class="line">	</span><br><span class="line">	<span class="comment">// 获取SpringApplicationRunListeners 详解内容下面会进行解说</span></span><br><span class="line">	SpringApplicationRunListeners listeners = getRunListeners(args);</span><br><span class="line">	listeners.starting();</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">try</span> &#123;</span><br><span class="line">	</span><br><span class="line">	    <span class="comment">// 构造一个应用程序参数持有类</span></span><br><span class="line">		ApplicationArguments applicationArguments = <span class="keyword">new</span> DefaultApplicationArguments(args);</span><br><span class="line">				</span><br><span class="line">		<span class="comment">//准备运行的环境</span></span><br><span class="line">		ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);</span><br><span class="line">		Banner printedBanner = printBanner(environment);</span><br><span class="line">		</span><br><span class="line">		<span class="comment">// 创建Spring容器</span></span><br><span class="line">		context = createApplicationContext();</span><br><span class="line">		</span><br><span class="line">		analyzers = <span class="keyword">new</span> FailureAnalyzers(context);</span><br><span class="line">		prepareContext(context, environment, listeners, applicationArguments, printedBanner);</span><br><span class="line">		refreshContext(context);</span><br><span class="line">		</span><br><span class="line">		<span class="comment">// 容器创建完成之后执行额外一些操作</span></span><br><span class="line">		afterRefresh(context, applicationArguments);</span><br><span class="line">		listeners.finished(context, <span class="keyword">null</span>);</span><br><span class="line">		stopWatch.stop();</span><br><span class="line">		<span class="keyword">if</span> (<span class="keyword">this</span>.logStartupInfo) &#123;</span><br><span class="line">			<span class="keyword">new</span> StartupInfoLogger(<span class="keyword">this</span>.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">return</span> context;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">catch</span> (Throwable ex) &#123;</span><br><span class="line">		handleRunFailure(context, listeners, analyzers, ex);</span><br><span class="line">		<span class="keyword">throw</span> <span class="keyword">new</span> IllegalStateException(ex);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<ul>
<li>new StopWatch(); // 构造一个任务执行观察器 该类是Spring提供的一个计时器类</li>
</ul>
</blockquote>
<h5 id="3-1简述configureHeadlessProperty-方法"><a href="#3-1简述configureHeadlessProperty-方法" class="headerlink" title="3-1简述configureHeadlessProperty();方法"></a>3-1简述configureHeadlessProperty();方法</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">configureHeadlessProperty</span><span class="params">()</span> </span>&#123;</span><br><span class="line">	System.setProperty(SYSTEM_PROPERTY_JAVA_AWT_HEADLESS, System.getProperty(</span><br><span class="line">			SYSTEM_PROPERTY_JAVA_AWT_HEADLESS, Boolean.toString(<span class="keyword">this</span>.headless)));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<ul>
<li>设置系统属性java.awt.headless，这里设置为true，表示运行在服务器端，在没有显示器和鼠标键盘的模式下工作，模拟输入输出设备功能</li>
</ul>
</blockquote>
<h6 id="3-2简述getRunListeners-args-方法"><a href="#3-2简述getRunListeners-args-方法" class="headerlink" title="3-2简述getRunListeners(args);方法"></a>3-2简述getRunListeners(args);方法</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">private</span> SpringApplicationRunListeners <span class="title">getRunListeners</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		Class&lt;?&gt;[] types = <span class="keyword">new</span> Class&lt;?&gt;[] &#123; SpringApplication.class, String[].class &#125;;</span><br><span class="line">		<span class="keyword">return</span> <span class="keyword">new</span> SpringApplicationRunListeners(logger, getSpringFactoriesInstances(</span><br><span class="line">				SpringApplicationRunListener.class, types, <span class="keyword">this</span>, args));</span><br><span class="line">	&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<ul>
<li>获取SpringApplicationRunListeners通过getRunListeners方法获取SpringApplicationRunListeners对象，这个对象是一个SpringBoot事件广播器的管理者，它可以包含多个SpringApplicationRunListener，SpringApplication类中使用它们来间接调用ApplicationListener</li>
<li>在classpath下的JAR文件中包含的/META/spring.factories文件里找到org.springframework.boot.SpringApplicationRunListener对应的属性，然后实例化并排序;</li>
<li>jar:file:/D:\maven\maven_resource\org\springframework\boot\spring-boot\1.5.11.RELEASE/spring-boot-1.5.11.RELEASE.jar!/META-INF/spring.factories文件找到如下配置:<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">&gt; # Run Listeners</span><br><span class="line">&gt; org.springframework.boot.SpringApplicationRunListener=\</span><br><span class="line">&gt; org.springframework.boot.context.event.EventPublishingRunListener</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&gt; - 就是EventPublishingRunListener，然后作为参数调用SpringApplicationRunListeners的构造函数，创建对象并返回，EventPublishingRunListener对象的实例被添加到SpringApplicationRunListeners对象的listeners属性中</span><br><span class="line"></span><br><span class="line">###### 3-2-1 EventPublishingRunListener 看看究竟是什么鬼？</span><br><span class="line">```java</span><br><span class="line">public EventPublishingRunListener(SpringApplication application, String[] args) &#123;</span><br><span class="line">    this.application = application;</span><br><span class="line">    this.args = args;</span><br><span class="line">    this.initialMulticaster = new SimpleApplicationEventMulticaster();</span><br><span class="line">    for (ApplicationListener&lt;?&gt; listener : application.getListeners()) &#123;</span><br><span class="line">        this.initialMulticaster.addApplicationListener(listener);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ul>
</blockquote>
<blockquote>
<ul>
<li>源码中可以看出 我们new SimpleApplicationEventMulticaster(); 这是一个事件广播器，它实现了SpringApplicationRunListener接口；我们看下SpringApplicationRunListener接口的定义</li>
</ul>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Listener for the &#123;<span class="doctag">@link</span> SpringApplication&#125; &#123;<span class="doctag">@code</span> run&#125; method.</span></span><br><span class="line"><span class="comment"> * &#123;<span class="doctag">@link</span> SpringApplicationRunListener&#125;s are loaded via the &#123;<span class="doctag">@link</span> SpringFactoriesLoader&#125;</span></span><br><span class="line"><span class="comment"> * and should declare a public constructor that accepts a &#123;<span class="doctag">@link</span> SpringApplication&#125;</span></span><br><span class="line"><span class="comment"> * instance and a &#123;<span class="doctag">@code</span> String[]&#125; of arguments. A new</span></span><br><span class="line"><span class="comment"> * &#123;<span class="doctag">@link</span> SpringApplicationRunListener&#125; instance will be created for each run.</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> Phillip Webb</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> Dave Syer</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">SpringApplicationRunListener</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * Called immediately when the run method has first started. Can be used for very</span></span><br><span class="line"><span class="comment">	 * early initialization. 首次启动run方法时立即调用。, 可用于非常早期初始化</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="function"><span class="keyword">void</span> <span class="title">starting</span><span class="params">()</span></span>;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * Called once the environment has been prepared, but before the</span></span><br><span class="line"><span class="comment">	 * &#123;<span class="doctag">@link</span> ApplicationContext&#125; has been created.</span></span><br><span class="line"><span class="comment">	 * <span class="doctag">@param</span> environment the environment </span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="function"><span class="keyword">void</span> <span class="title">environmentPrepared</span><span class="params">(ConfigurableEnvironment environment)</span></span>;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * Called once the &#123;<span class="doctag">@link</span> ApplicationContext&#125; has been created and prepared, but</span></span><br><span class="line"><span class="comment">	 * before sources have been loaded.</span></span><br><span class="line"><span class="comment">	 * <span class="doctag">@param</span> context the application context</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="function"><span class="keyword">void</span> <span class="title">contextPrepared</span><span class="params">(ConfigurableApplicationContext context)</span></span>;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * Called once the application context has been loaded but before it has been</span></span><br><span class="line"><span class="comment">	 * refreshed.  在应用程序上下文加载之后但在刷新之前调用</span></span><br><span class="line"><span class="comment">	 * <span class="doctag">@param</span> context the application context</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="function"><span class="keyword">void</span> <span class="title">contextLoaded</span><span class="params">(ConfigurableApplicationContext context)</span></span>;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * Called immediately before the run method finishes.</span></span><br><span class="line"><span class="comment">	 * <span class="doctag">@param</span> context the application context or null if a failure occurred before the</span></span><br><span class="line"><span class="comment">	 * context was created</span></span><br><span class="line"><span class="comment">	 * <span class="doctag">@param</span> exception any run exception or null if run completed successfully.</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="function"><span class="keyword">void</span> <span class="title">finished</span><span class="params">(ConfigurableApplicationContext context, Throwable exception)</span></span>;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<ul>
<li>通过接口定义，以及方法注解，在其SpringBoot的生命周期的几个阶段定义了相应的事件，调用实际的ApplicationListener的onApplicationEvent方法</li>
</ul>
</blockquote>
<h5 id="3-3-OK-继续回来run-方法里面-getRunListeners-args-方法之后-调用了listeners-starting"><a href="#3-3-OK-继续回来run-方法里面-getRunListeners-args-方法之后-调用了listeners-starting" class="headerlink" title="3-3 OK 继续回来run()方法里面 getRunListeners(args);方法之后 调用了listeners.starting();"></a>3-3 OK 继续回来run()方法里面 getRunListeners(args);方法之后 调用了listeners.starting();</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">starting</span><span class="params">()</span> </span>&#123;</span><br><span class="line">	<span class="keyword">for</span> (SpringApplicationRunListener listener : <span class="keyword">this</span>.listeners) &#123;</span><br><span class="line">		listener.starting();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// listener.starting(); </span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">EventPublishingRunListener</span> <span class="keyword">implements</span> <span class="title">SpringApplicationRunListener</span>, <span class="title">Ordered</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">	<span class="meta">@SuppressWarnings</span>(<span class="string">"deprecation"</span>)</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">starting</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.initialMulticaster</span><br><span class="line">				.multicastEvent(<span class="keyword">new</span> ApplicationStartedEvent(<span class="keyword">this</span>.application, <span class="keyword">this</span>.args));</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<ul>
<li>里面就是一个for循环 遍历所有的SpringApplicationRunListener，并调用起starting()方法；其实我们通过spring.factories文件可以知晓这里就是EventPublishingRunListener，调用它的starting方法</li>
</ul>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="meta">@SuppressWarnings</span>(<span class="string">"deprecation"</span>)</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">starting</span><span class="params">()</span> </span>&#123;</span><br><span class="line">	<span class="keyword">this</span>.initialMulticaster.multicastEvent(<span class="keyword">new</span> ApplicationStartedEvent(<span class="keyword">this</span>.application, <span class="keyword">this</span>.args));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<ul>
<li>使用广播器(initialMulticaster==&gt;SimpleApplicationEventMulticaster)去广播事件，创建一个ApplicationStartedEvent对象作为参数;</li>
<li>multicastEvent方法里面首先获取到一个ApplicationListener的集合，然后遍历ApplicationListener的集合，依次调用其invokeListener(listener, event);方法</li>
</ul>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">multicastEvent</span><span class="params">(ApplicationEvent event)</span> </span>&#123;</span><br><span class="line">	multicastEvent(event, resolveDefaultEventType(event));</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">multicastEvent</span><span class="params">(<span class="keyword">final</span> ApplicationEvent event, ResolvableType eventType)</span> </span>&#123;</span><br><span class="line">	ResolvableType type = (eventType != <span class="keyword">null</span> ? eventType : resolveDefaultEventType(event));</span><br><span class="line">	<span class="keyword">for</span> (<span class="keyword">final</span> ApplicationListener&lt;?&gt; listener : getApplicationListeners(event, type)) &#123;</span><br><span class="line">		Executor executor = getTaskExecutor();</span><br><span class="line">		<span class="keyword">if</span> (executor != <span class="keyword">null</span>) &#123;</span><br><span class="line">			executor.execute(<span class="keyword">new</span> Runnable() &#123;</span><br><span class="line">				<span class="meta">@Override</span></span><br><span class="line">				<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">					invokeListener(listener, event);</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;);</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">else</span> &#123;</span><br><span class="line">			invokeListener(listener, event);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">invokeListener</span><span class="params">(ApplicationListener&lt;?&gt; listener, ApplicationEvent event)</span> </span>&#123;</span><br><span class="line">	ErrorHandler errorHandler = getErrorHandler();</span><br><span class="line">	<span class="keyword">if</span> (errorHandler != <span class="keyword">null</span>) &#123;</span><br><span class="line">		<span class="keyword">try</span> &#123;</span><br><span class="line">			doInvokeListener(listener, event);</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">catch</span> (Throwable err) &#123;</span><br><span class="line">			errorHandler.handleError(err);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span> &#123;</span><br><span class="line">		doInvokeListener(listener, event);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">doInvokeListener</span><span class="params">(ApplicationListener listener, ApplicationEvent event)</span> </span>&#123;</span><br><span class="line">	<span class="keyword">try</span> &#123;</span><br><span class="line">		listener.onApplicationEvent(event);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">catch</span> (ClassCastException ex) &#123;</span><br><span class="line">		...<span class="comment">//省略</span></span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<ul>
<li>其实invokeListener方法内部则会调用ApplicationListener对象的onApplicationEvent方法</li>
</ul>
</blockquote>
<h5 id="3-5-DefaultApplicationArguments-args"><a href="#3-5-DefaultApplicationArguments-args" class="headerlink" title="3-5 DefaultApplicationArguments(args);"></a>3-5 DefaultApplicationArguments(args);</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="title">DefaultApplicationArguments</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">	Assert.notNull(args, <span class="string">"Args must not be null"</span>);</span><br><span class="line">	<span class="keyword">this</span>.source = <span class="keyword">new</span> Source(args);</span><br><span class="line">	<span class="keyword">this</span>.args = args;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<ul>
<li>创建一个DefaultApplicationArguments对象，它持有着args参数，就是main函数传进来的参数</li>
</ul>
</blockquote>
<h5 id="3-6-prepareEnvironment-listeners-applicationArguments"><a href="#3-6-prepareEnvironment-listeners-applicationArguments" class="headerlink" title="3-6 prepareEnvironment(listeners, applicationArguments);"></a>3-6 prepareEnvironment(listeners, applicationArguments);</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">private</span> ConfigurableEnvironment <span class="title">prepareEnvironment</span><span class="params">(SpringApplicationRunListeners listeners, ApplicationArguments applicationArguments)</span> </span>&#123;</span><br><span class="line">	<span class="comment">// Create and configure the environment</span></span><br><span class="line">	ConfigurableEnvironment environment = getOrCreateEnvironment();</span><br><span class="line">	configureEnvironment(environment, applicationArguments.getSourceArgs());</span><br><span class="line">	listeners.environmentPrepared(environment);</span><br><span class="line">	<span class="keyword">if</span> (!<span class="keyword">this</span>.webEnvironment) &#123;</span><br><span class="line">		environment = <span class="keyword">new</span> EnvironmentConverter(getClassLoader())</span><br><span class="line">				.convertToStandardEnvironmentIfNecessary(environment);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> environment;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<ul>
<li>调用prepareEnvironment方法,准备运行的环境，比如开发环境dev，测试环境test，还是生产环境prd，然后根据环境解析不同的配置文件</li>
</ul>
</blockquote>
<h6 id="3-6-1-简述getOrCreateEnvironment-方法"><a href="#3-6-1-简述getOrCreateEnvironment-方法" class="headerlink" title="3-6-1 简述getOrCreateEnvironment();方法"></a>3-6-1 简述getOrCreateEnvironment();方法</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">private</span> ConfigurableEnvironment <span class="title">getOrCreateEnvironment</span><span class="params">()</span> </span>&#123;</span><br><span class="line">	<span class="keyword">if</span> (<span class="keyword">this</span>.environment != <span class="keyword">null</span>) &#123;</span><br><span class="line">		<span class="keyword">return</span> <span class="keyword">this</span>.environment;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span> (<span class="keyword">this</span>.webEnvironment) &#123;</span><br><span class="line">		<span class="keyword">return</span> <span class="keyword">new</span> StandardServletEnvironment();</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="keyword">new</span> StandardEnvironment();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<ul>
<li>getOrCreateEnvironment方法会检查之前设置的webEnvironment属性，如果是web程序，那么创建一个StandardServletEnvironment对象并返回，如果不是web程序，那么创建一个StandardEnvironment对象并返回;</li>
</ul>
</blockquote>
<h6 id="3-6-2-configureEnvironment-…-方法"><a href="#3-6-2-configureEnvironment-…-方法" class="headerlink" title="3-6-2 configureEnvironment(…);方法"></a>3-6-2 configureEnvironment(…);方法</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Template method delegating to</span></span><br><span class="line"><span class="comment"> * &#123;<span class="doctag">@link</span> #configurePropertySources(ConfigurableEnvironment, String[])&#125; and</span></span><br><span class="line"><span class="comment"> * &#123;<span class="doctag">@link</span> #configureProfiles(ConfigurableEnvironment, String[])&#125; in that order.</span></span><br><span class="line"><span class="comment"> * Override this method for complete control over Environment customization, or one of</span></span><br><span class="line"><span class="comment"> * the above for fine-grained control over property sources or profiles, respectively.</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> environment this application's environment</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> args arguments passed to the &#123;<span class="doctag">@code</span> run&#125; method</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@see</span> #configureProfiles(ConfigurableEnvironment, String[])</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@see</span> #configurePropertySources(ConfigurableEnvironment, String[])</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">configureEnvironment</span><span class="params">(ConfigurableEnvironment environment,</span></span></span><br><span class="line"><span class="function"><span class="params">		String[] args)</span> </span>&#123;</span><br><span class="line">	configurePropertySources(environment, args);</span><br><span class="line">	configureProfiles(environment, args);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<ul>
<li>对environment做进一步的配置，调用configurePropertySources和configureProfiles，比如main函数传进来参数”–spring.profiles.active=dev”，那么会在configurePropertySources方法里被添加到CommandLineArgs对象的optionArgs属性，不是以”–”开头的参数会被添加到nonOptionArgs属性。而configureProfiles方法这个时候就会获取到dev作为当前有效的profile并添加到environment的activeProfiles属性中</li>
</ul>
</blockquote>
<h6 id="3-6-3-listeners-environmentPrepared-environment-方法"><a href="#3-6-3-listeners-environmentPrepared-environment-方法" class="headerlink" title="3-6-3 listeners.environmentPrepared(environment);方法"></a>3-6-3 listeners.environmentPrepared(environment);方法</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">environmentPrepared</span><span class="params">(ConfigurableEnvironment environment)</span> </span>&#123;</span><br><span class="line">	<span class="keyword">for</span> (SpringApplicationRunListener listener : <span class="keyword">this</span>.listeners) &#123;</span><br><span class="line">		listener.environmentPrepared(environment);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<ul>
<li>看到这个方法就明白了 是否想起了我们上述注册的几个事件监听，对！当你环境准备好了，此时应该是还没有准备好！那就发布送(广播)事件通知监听者吧！调用listeners的environmentPrepared方法，发布一个ApplicationEnvironmentPreparedEvent事件，通过事件广播器，依次调用每个ApplicationListener对象的onApplicationEvent方法，所以我们很有必要去了解一下，这些事件监听者都做了写什么事情！；</li>
</ul>
</blockquote>
<h6 id="3-6-3-1-我们看下onApplicationEvent方法"><a href="#3-6-3-1-我们看下onApplicationEvent方法" class="headerlink" title="3-6-3-1 我们看下onApplicationEvent方法"></a>3-6-3-1 我们看下onApplicationEvent方法</h6><blockquote>
<ul>
<li>首先我们了解下这个方法的定义,ApplicationListener接口内部只有这一个方法。</li>
</ul>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">ApplicationListener</span>&lt;<span class="title">E</span> <span class="keyword">extends</span> <span class="title">ApplicationEvent</span>&gt; <span class="keyword">extends</span> <span class="title">EventListener</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * Handle an application event.</span></span><br><span class="line"><span class="comment">	 * <span class="doctag">@param</span> event the event to respond to</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="function"><span class="keyword">void</span> <span class="title">onApplicationEvent</span><span class="params">(E event)</span></span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<ul>
<li>它有很多的实现，根据不同的事件去做不同的事情，OK 我们本例是environmentPrepared(ConfigurableEnvironment environment)，所以和准备环境又换的事情会触发！</li>
<li>简要分析ConfigFileApplicationListener的onApplicationEvent方法</li>
</ul>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//内部调用了下述方法，最终可知是调用了loadPostProcessors()</span></span><br><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onApplicationEvent</span><span class="params">(ApplicationEvent event)</span> </span>&#123;</span><br><span class="line">	<span class="keyword">if</span> (event <span class="keyword">instanceof</span> ApplicationEnvironmentPreparedEvent) &#123;</span><br><span class="line">		onApplicationEnvironmentPreparedEvent(</span><br><span class="line">				(ApplicationEnvironmentPreparedEvent) event);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span> (event <span class="keyword">instanceof</span> ApplicationPreparedEvent) &#123;</span><br><span class="line">		onApplicationPreparedEvent(event);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//跳转此方法</span></span><br><span class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">onApplicationEnvironmentPreparedEvent</span><span class="params">(ApplicationEnvironmentPreparedEvent event)</span> </span>&#123;</span><br><span class="line">	List&lt;EnvironmentPostProcessor&gt; postProcessors = loadPostProcessors();</span><br><span class="line">	postProcessors.add(<span class="keyword">this</span>);</span><br><span class="line">	AnnotationAwareOrderComparator.sort(postProcessors);</span><br><span class="line">	<span class="keyword">for</span> (EnvironmentPostProcessor postProcessor : postProcessors) &#123;</span><br><span class="line">		postProcessor.postProcessEnvironment(event.getEnvironment(),</span><br><span class="line">				event.getSpringApplication());</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">onApplicationPreparedEvent</span><span class="params">(ApplicationEvent event)</span> </span>&#123;</span><br><span class="line">	<span class="keyword">this</span>.logger.replayTo(ConfigFileApplicationListener.class);</span><br><span class="line">	addPostProcessors(((ApplicationPreparedEvent) event).getApplicationContext());</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<ul>
<li>loadPostProcessors() ：</li>
<li>读取classpath下的JAR文件中包含的/META/spring.factories文件里找到org.springframework.boot.env.EnvironmentPostProcessor对应的属性，然后实例化并排序，设置到postProcessors变量，在把当前对象(ConfigFileApplicationListener的实例)也添加到postProcessors变量中；<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">&gt; # Environment Post Processors</span><br><span class="line">&gt; org.springframework.boot.env.EnvironmentPostProcessor=\</span><br><span class="line">&gt; org.springframework.boot.cloud.CloudFoundryVcapEnvironmentPostProcessor,\</span><br><span class="line">&gt; org.springframework.boot.env.SpringApplicationJsonEnvironmentPostProcessor</span><br><span class="line"></span><br><span class="line">&gt; - 然后对postProcessors排序，遍历postProcessors，依次调用它们的postProcessEnvironment方法。</span><br><span class="line"></span><br><span class="line">```java</span><br><span class="line">@Override</span><br><span class="line">public void postProcessEnvironment(ConfigurableEnvironment environment,</span><br><span class="line">		SpringApplication application) &#123;</span><br><span class="line">	addPropertySources(environment, application.getResourceLoader());</span><br><span class="line">	configureIgnoreBeanInfo(environment);</span><br><span class="line">	bindToSpringApplication(environment, application);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ul>
</blockquote>
<blockquote>
<ul>
<li>addPropertySources : 方法最终会调用到ConfigFileApplicationListener的内部类Loader的load方法 其作用就是最终解析我们配置的application.yml/application-xxx.yml</li>
</ul>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">load</span><span class="params">()</span> </span>&#123;</span><br><span class="line">	<span class="keyword">this</span>.propertiesLoader = <span class="keyword">new</span> PropertySourcesLoader();</span><br><span class="line">	<span class="keyword">this</span>.activatedProfiles = <span class="keyword">false</span>;</span><br><span class="line">	<span class="keyword">this</span>.profiles = Collections.asLifoQueue(<span class="keyword">new</span> LinkedList&lt;Profile&gt;());</span><br><span class="line">	<span class="keyword">this</span>.processedProfiles = <span class="keyword">new</span> LinkedList&lt;Profile&gt;();</span><br><span class="line"></span><br><span class="line">	<span class="comment">// Pre-existing active profiles set via Environment.setActiveProfiles()</span></span><br><span class="line">	<span class="comment">// are additional profiles and config files are allowed to add more if</span></span><br><span class="line">	<span class="comment">// they want to, so don't call addActiveProfiles() here.</span></span><br><span class="line">	Set&lt;Profile&gt; initialActiveProfiles = initializeActiveProfiles();</span><br><span class="line">	<span class="keyword">this</span>.profiles.addAll(getUnprocessedActiveProfiles(initialActiveProfiles));</span><br><span class="line">	<span class="keyword">if</span> (<span class="keyword">this</span>.profiles.isEmpty()) &#123;</span><br><span class="line">		<span class="keyword">for</span> (String defaultProfileName : <span class="keyword">this</span>.environment.getDefaultProfiles()) &#123;</span><br><span class="line">			Profile defaultProfile = <span class="keyword">new</span> Profile(defaultProfileName, <span class="keyword">true</span>);</span><br><span class="line">			<span class="keyword">if</span> (!<span class="keyword">this</span>.profiles.contains(defaultProfile)) &#123;</span><br><span class="line">				<span class="keyword">this</span>.profiles.add(defaultProfile);</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">// The default profile for these purposes is represented as null. We add it</span></span><br><span class="line">	<span class="comment">// last so that it is first out of the queue (active profiles will then</span></span><br><span class="line">	<span class="comment">// override any settings in the defaults when the list is reversed later).</span></span><br><span class="line">	<span class="keyword">this</span>.profiles.add(<span class="keyword">null</span>);</span><br><span class="line"></span><br><span class="line">	<span class="keyword">while</span> (!<span class="keyword">this</span>.profiles.isEmpty()) &#123;</span><br><span class="line">		Profile profile = <span class="keyword">this</span>.profiles.poll();</span><br><span class="line">		<span class="keyword">for</span> (String location : getSearchLocations()) &#123;</span><br><span class="line">			<span class="keyword">if</span> (!location.endsWith(<span class="string">"/"</span>)) &#123;</span><br><span class="line">				<span class="comment">// location is a filename already, so don't search for more</span></span><br><span class="line">				<span class="comment">// filenames</span></span><br><span class="line">				load(location, <span class="keyword">null</span>, profile);</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="keyword">else</span> &#123;</span><br><span class="line">				<span class="keyword">for</span> (String name : getSearchNames()) &#123;</span><br><span class="line">					load(location, name, profile);</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">this</span>.processedProfiles.add(profile);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	addConfigurationProperties(<span class="keyword">this</span>.propertiesLoader.getPropertySources());</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h6 id="3-6-3-2看下getSearchLocations-方法；"><a href="#3-6-3-2看下getSearchLocations-方法；" class="headerlink" title="3-6-3-2看下getSearchLocations()方法；"></a>3-6-3-2看下getSearchLocations()方法；</h6><blockquote>
<ul>
<li>首先会检查当前环境是否配置了spring.config.location；</li>
<li>如果没有，那么会使用默认的搜索路径classpath:/,classpath:/config/,file:./,file:./config/</li>
<li>注意由于方法里调用Collections.reverse方法进行倒序，也就是先查找file:./config/，在查找file:./，再是classpath:/config/，最后查找classpath:/</li>
<li>所以这里可以通过代码我们知道 我们可以在通过设置spring.config.location 更改yml文件的路径，spring.config.name 更改yml的name 默认是application</li>
</ul>
</blockquote>
<h6 id="3-6-3-3-load-具体加载文件的方法"><a href="#3-6-3-3-load-具体加载文件的方法" class="headerlink" title="3-6-3-3 load() 具体加载文件的方法"></a>3-6-3-3 load() 具体加载文件的方法</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">load</span><span class="params">(String location, String name, Profile profile)</span> </span>&#123;</span><br><span class="line">	String group = <span class="string">"profile="</span> + (profile == <span class="keyword">null</span> ? <span class="string">""</span> : profile);</span><br><span class="line">	<span class="keyword">if</span> (!StringUtils.hasText(name)) &#123;</span><br><span class="line">		<span class="comment">// Try to load directly from the location</span></span><br><span class="line">		loadIntoGroup(group, location, profile);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span> &#123;</span><br><span class="line">		<span class="comment">// Search for a file with the given name</span></span><br><span class="line">		<span class="keyword">for</span> (String ext : <span class="keyword">this</span>.propertiesLoader.getAllFileExtensions()) &#123;</span><br><span class="line">			<span class="keyword">if</span> (profile != <span class="keyword">null</span>) &#123;</span><br><span class="line">				<span class="comment">// Try the profile-specific file</span></span><br><span class="line">				loadIntoGroup(group, location + name + <span class="string">"-"</span> + profile + <span class="string">"."</span> + ext,</span><br><span class="line">						<span class="keyword">null</span>);</span><br><span class="line">				<span class="keyword">for</span> (Profile processedProfile : <span class="keyword">this</span>.processedProfiles) &#123;</span><br><span class="line">					<span class="keyword">if</span> (processedProfile != <span class="keyword">null</span>) &#123;</span><br><span class="line">						loadIntoGroup(group, location + name + <span class="string">"-"</span></span><br><span class="line">								+ processedProfile + <span class="string">"."</span> + ext, profile);</span><br><span class="line">					&#125;</span><br><span class="line">				&#125;</span><br><span class="line">				<span class="comment">// Sometimes people put "spring.profiles: dev" in</span></span><br><span class="line">				<span class="comment">// application-dev.yml (gh-340). Arguably we should try and error</span></span><br><span class="line">				<span class="comment">// out on that, but we can be kind and load it anyway.</span></span><br><span class="line">				loadIntoGroup(group, location + name + <span class="string">"-"</span> + profile + <span class="string">"."</span> + ext,</span><br><span class="line">						profile);</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="comment">// Also try the profile-specific section (if any) of the normal file</span></span><br><span class="line">			loadIntoGroup(group, location + name + <span class="string">"."</span> + ext, profile);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<ul>
<li>this.propertiesLoader.getAllFileExtensions()；</li>
<li>方法获取配置文件后缀名，分别是在PropertiesPropertySourceLoader类和YamlPropertySourceLoader类中getFileExtensions方法定义的<br>具体有两种实现：</li>
</ul>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Strategy to load '.properties' files into a &#123;<span class="doctag">@link</span> PropertySource&#125;.</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> Dave Syer</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> Phillip Webb</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">PropertiesPropertySourceLoader</span> <span class="keyword">implements</span> <span class="title">PropertySourceLoader</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="keyword">public</span> String[] getFileExtensions() &#123;</span><br><span class="line">		<span class="keyword">return</span> <span class="keyword">new</span> String[] &#123; <span class="string">"properties"</span>, <span class="string">"xml"</span> &#125;;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Strategy to load '.yml' (or '.yaml') files into a &#123;<span class="doctag">@link</span> PropertySource&#125;.</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> Dave Syer</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> Phillip Webb</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> Andy Wilkinson</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">YamlPropertySourceLoader</span> <span class="keyword">implements</span> <span class="title">PropertySourceLoader</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="keyword">public</span> String[] getFileExtensions() &#123;</span><br><span class="line">		<span class="keyword">return</span> <span class="keyword">new</span> String[] &#123; <span class="string">"yml"</span>, <span class="string">"yaml"</span> &#125;;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h6 id="3-6-3-4-紧接着loadIntoGroup-又调用doLoadIntoGroup方法，这个方法会检查配置文件是否存在-该方法就不再贴出来了-找到后进行解析，"><a href="#3-6-3-4-紧接着loadIntoGroup-又调用doLoadIntoGroup方法，这个方法会检查配置文件是否存在-该方法就不再贴出来了-找到后进行解析，" class="headerlink" title="3-6-3-4 紧接着loadIntoGroup() : 又调用doLoadIntoGroup方法，这个方法会检查配置文件是否存在,该方法就不再贴出来了; 找到后进行解析，"></a>3-6-3-4 紧接着loadIntoGroup() : 又调用doLoadIntoGroup方法，这个方法会检查配置文件是否存在,该方法就不再贴出来了; 找到后进行解析，</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> PropertySource&lt;?&gt; doLoadIntoGroup(String identifier, String location,</span><br><span class="line">				Profile profile) <span class="keyword">throws</span> IOException &#123;</span><br><span class="line">	Resource resource = <span class="keyword">this</span>.resourceLoader.getResource(location);</span><br><span class="line">	PropertySource&lt;?&gt; propertySource = <span class="keyword">null</span>;</span><br><span class="line">	StringBuilder msg = <span class="keyword">new</span> StringBuilder();</span><br><span class="line">	<span class="keyword">if</span> (resource != <span class="keyword">null</span> &amp;&amp; resource.exists()) &#123;</span><br><span class="line">		String name = <span class="string">"applicationConfig: ["</span> + location + <span class="string">"]"</span>;</span><br><span class="line">		String group = <span class="string">"applicationConfig: ["</span> + identifier + <span class="string">"]"</span>;</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//</span></span><br><span class="line">		propertySource = <span class="keyword">this</span>.propertiesLoader.load(resource, group, name,</span><br><span class="line">				(profile == <span class="keyword">null</span> ? <span class="keyword">null</span> : profile.getName()));</span><br><span class="line">		<span class="keyword">if</span> (propertySource != <span class="keyword">null</span>) &#123;</span><br><span class="line">			msg.append(<span class="string">"Loaded "</span>);</span><br><span class="line">			handleProfileProperties(propertySource);</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">else</span> &#123;</span><br><span class="line">			msg.append(<span class="string">"Skipped (empty) "</span>);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span> &#123;</span><br><span class="line">		msg.append(<span class="string">"Skipped "</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	msg.append(<span class="string">"config file "</span>);</span><br><span class="line">	msg.append(getResourceDescription(location, resource));</span><br><span class="line">	<span class="keyword">if</span> (profile != <span class="keyword">null</span>) &#123;</span><br><span class="line">		msg.append(<span class="string">" for profile "</span>).append(profile);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span> (resource == <span class="keyword">null</span> || !resource.exists()) &#123;</span><br><span class="line">		msg.append(<span class="string">" resource not found"</span>);</span><br><span class="line">		<span class="keyword">this</span>.logger.trace(msg);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span> &#123;</span><br><span class="line">		<span class="keyword">this</span>.logger.debug(msg);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> propertySource;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h6 id="3-6-3-5-this-propertiesLoader-load-resource-group-name-profile-null-null-profile-getName"><a href="#3-6-3-5-this-propertiesLoader-load-resource-group-name-profile-null-null-profile-getName" class="headerlink" title="3-6-3-5 this.propertiesLoader.load(resource, group, name,(profile == null ? null : profile.getName()));"></a>3-6-3-5 this.propertiesLoader.load(resource, group, name,(profile == null ? null : profile.getName()));</h6><p>OK 其实我们根据上面的内容内容就应该知道 初次会进行两个格式的配置文件进行区分PropertiesPropertySourceLoader和YamlPropertySourceLoader，先检查是否该由此loader去解析，检查的规则就是看文件后缀名是否在getFileExtensions方法返回的数组中存在，很显然，这里应该由YamlPropertySourceLoader去解析<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Load the profile-specific properties from the specified resource (if any), give the</span></span><br><span class="line"><span class="comment"> * name provided and add it to a group of property sources identified by the group</span></span><br><span class="line"><span class="comment"> * name. Property sources are added to the end of a group, but new groups are added as</span></span><br><span class="line"><span class="comment"> * the first in the chain being assembled. This means the normal sequence of calls is</span></span><br><span class="line"><span class="comment"> * to first create the group for the default (null) profile, and then add specific</span></span><br><span class="line"><span class="comment"> * groups afterwards (with the highest priority last). Property resolution from the</span></span><br><span class="line"><span class="comment"> * resulting sources will consider all keys for a given group first and then move to</span></span><br><span class="line"><span class="comment"> * the next group.</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> resource the source resource (may be &#123;<span class="doctag">@code</span> null&#125;).</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> group an identifier for the group that this source belongs to</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> name the root property name (may be &#123;<span class="doctag">@code</span> null&#125;).</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> profile a specific profile to load or &#123;<span class="doctag">@code</span> null&#125; to load the default.</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> the loaded property source or &#123;<span class="doctag">@code</span> null&#125;</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@throws</span> IOException if the source cannot be loaded</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> PropertySource&lt;?&gt; load(Resource resource, String group, String name,</span><br><span class="line">		String profile) <span class="keyword">throws</span> IOException &#123;</span><br><span class="line">	<span class="keyword">if</span> (isFile(resource)) &#123;</span><br><span class="line">		String sourceName = generatePropertySourceName(name, profile);</span><br><span class="line">		<span class="keyword">for</span> (PropertySourceLoader loader : <span class="keyword">this</span>.loaders) &#123;</span><br><span class="line">			<span class="keyword">if</span> (canLoadFileExtension(loader, resource)) &#123;</span><br><span class="line">				PropertySource&lt;?&gt; specific = loader.load(sourceName, resource,</span><br><span class="line">						profile);</span><br><span class="line">				addPropertySource(group, specific, profile);</span><br><span class="line">				<span class="keyword">return</span> specific;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h6 id="3-6-3-6-解析完成：开始调用handleProfileProperties-propertySource"><a href="#3-6-3-6-解析完成：开始调用handleProfileProperties-propertySource" class="headerlink" title="3-6-3-6 解析完成：开始调用handleProfileProperties(propertySource);"></a>3-6-3-6 解析完成：开始调用handleProfileProperties(propertySource);</h6><blockquote>
<p>调用了一个handleProfileProperties方法，这个方法里又会调用maybeActivateProfiles方法，此方法的addProfiles方法会把解析到的dev添加到profiles中去，removeUnprocessedDefaultProfiles方法会删除之前添加的default(此时只剩下dev)，如果配置了include，也会被解析，然后再次遍历file:./config/，file:./，classpath:/config/，classpath:/，这次是查找到application-dev.yml并解析，过程就不重复分析了</p>
</blockquote>
<h5 id="4-至此结束后，我们回到run-方法中。"><a href="#4-至此结束后，我们回到run-方法中。" class="headerlink" title="4. 至此结束后，我们回到run()方法中。"></a>4. 至此结束后，我们回到run()方法中。</h5><h5 id="4-1-Banner-printedBanner-printBanner-environment"><a href="#4-1-Banner-printedBanner-printBanner-environment" class="headerlink" title="4-1 Banner printedBanner = printBanner(environment);"></a>4-1 Banner printedBanner = printBanner(environment);</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">private</span> Banner <span class="title">printBanner</span><span class="params">(ConfigurableEnvironment environment)</span> </span>&#123;</span><br><span class="line">	<span class="keyword">if</span> (<span class="keyword">this</span>.bannerMode == Banner.Mode.OFF) &#123;</span><br><span class="line">		<span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	ResourceLoader resourceLoader = <span class="keyword">this</span>.resourceLoader != <span class="keyword">null</span> ? <span class="keyword">this</span>.resourceLoader</span><br><span class="line">			: <span class="keyword">new</span> DefaultResourceLoader(getClassLoader());</span><br><span class="line">	SpringApplicationBannerPrinter bannerPrinter = <span class="keyword">new</span> SpringApplicationBannerPrinter(</span><br><span class="line">			resourceLoader, <span class="keyword">this</span>.banner);</span><br><span class="line">	<span class="keyword">if</span> (<span class="keyword">this</span>.bannerMode == Mode.LOG) &#123;</span><br><span class="line">		<span class="keyword">return</span> bannerPrinter.print(environment, <span class="keyword">this</span>.mainApplicationClass, logger);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> bannerPrinter.print(environment, <span class="keyword">this</span>.mainApplicationClass, System.out);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">public</span> Banner <span class="title">print</span><span class="params">(Environment environment, Class&lt;?&gt; sourceClass, Log logger)</span> </span>&#123;</span><br><span class="line">	Banner banner = getBanner(environment, <span class="keyword">this</span>.fallbackBanner);</span><br><span class="line">	<span class="keyword">try</span> &#123;</span><br><span class="line">		logger.info(createStringFromBanner(banner, environment, sourceClass));</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">catch</span> (UnsupportedEncodingException ex) &#123;</span><br><span class="line">		logger.warn(<span class="string">"Failed to create String for banner"</span>, ex);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="keyword">new</span> PrintedBanner(banner, sourceClass);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">private</span> Banner <span class="title">getBanner</span><span class="params">(Environment environment, Banner definedBanner)</span> </span>&#123;</span><br><span class="line">	Banners banners = <span class="keyword">new</span> Banners();</span><br><span class="line">	banners.addIfNotNull(getImageBanner(environment));</span><br><span class="line">	banners.addIfNotNull(getTextBanner(environment));</span><br><span class="line">	<span class="keyword">if</span> (banners.hasAtLeastOneBanner()) &#123;</span><br><span class="line">		<span class="keyword">return</span> banners;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span> (<span class="keyword">this</span>.fallbackBanner != <span class="keyword">null</span>) &#123;</span><br><span class="line">		<span class="keyword">return</span> <span class="keyword">this</span>.fallbackBanner;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> DEFAULT_BANNER;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<ul>
<li>第一步，调用Banners的无参构造创建一个Banners的对象，然后我们先看一下SpringApplicationBannerPrinter类里定义的几个静态属性</li>
</ul>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">static</span> <span class="keyword">final</span> String BANNER_LOCATION_PROPERTY = <span class="string">"banner.location"</span>;</span><br><span class="line"><span class="keyword">static</span> <span class="keyword">final</span> String BANNER_IMAGE_LOCATION_PROPERTY = <span class="string">"banner.image.location"</span>;</span><br><span class="line"><span class="keyword">static</span> <span class="keyword">final</span> String DEFAULT_BANNER_LOCATION = <span class="string">"banner.txt"</span>;</span><br><span class="line"><span class="keyword">static</span> <span class="keyword">final</span> String[] IMAGE_EXTENSION = &#123; <span class="string">"gif"</span>, <span class="string">"jpg"</span>, <span class="string">"png"</span> &#125;;</span><br></pre></td></tr></table></figure>
<blockquote>
<ul>
<li>先看是否配置了系统属性banner.image.location，没有配置则在classpath中查找banner.gif，banner.jpg，banner.png，</li>
<li>如果找到，则创建一个ImageBanner对象并添加到Banners对象的banners属性中，该属性是一个List类型;</li>
<li>接下来看是否配置了banner.location属性，如果没有，则用默认的banner.txt，然后在classpath中查找banner.txt，如果找到，则创建一个ResourceBanner对象并添加到Banners对象的banners属性中;</li>
<li>最后，如果Banners对象的banners不为空，也就是至少找到了banner.gif，banner.jpg，banner.png，banner.txt其中的一个，那么返回该Banners对象，否则返回默认的SpringBootBanner对象,接下来根据返回的不同Banners类型的对象调用具体的printBanner实现方法，所以如果要想打印自定义的LOGO，要么你在classpath下添加图片，要么添加banner.txt的文本文件;</li>
<li>默认的SpringBootBanner会打印如下图:</li>
</ul>
</blockquote>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">  .   ____          _            __ _ _</span><br><span class="line"> /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \</span><br><span class="line">( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \</span><br><span class="line"> \\/  ___)| |_)| | | | | || (_| |  ) ) ) )</span><br><span class="line">  '  |____| .__|_| |_|_| |_\__, | / / / /</span><br><span class="line"> =========|_|==============|___/=/_/_/_/</span><br><span class="line"> :: Spring Boot ::       (v1.5.11.RELEASE)</span><br></pre></td></tr></table></figure>
<blockquote>
<ul>
<li>调用printBanner方法，打印SpringBoot的LOGO</li>
</ul>
</blockquote>
<h5 id="5-createAndRefreshContext-方法-创建容器的方法-该方法创建SpringBoot的上下文"><a href="#5-createAndRefreshContext-方法-创建容器的方法-该方法创建SpringBoot的上下文" class="headerlink" title="5. createAndRefreshContext()方法 创建容器的方法 该方法创建SpringBoot的上下文."></a>5. createAndRefreshContext()方法 创建容器的方法 该方法创建SpringBoot的上下文.</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Strategy method used to create the &#123;<span class="doctag">@link</span> ApplicationContext&#125;. By default this</span></span><br><span class="line"><span class="comment"> * method will respect any explicitly set application context or application context</span></span><br><span class="line"><span class="comment"> * class before falling back to a suitable default.</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> the application context (not yet refreshed)</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@see</span> #setApplicationContextClass(Class)</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">protected</span> ConfigurableApplicationContext <span class="title">createApplicationContext</span><span class="params">()</span> </span>&#123;</span><br><span class="line">	Class&lt;?&gt; contextClass = <span class="keyword">this</span>.applicationContextClass;</span><br><span class="line">	<span class="keyword">if</span> (contextClass == <span class="keyword">null</span>) &#123;</span><br><span class="line">		<span class="keyword">try</span> &#123;</span><br><span class="line">			contextClass = Class.forName(<span class="keyword">this</span>.webEnvironment ? DEFAULT_WEB_CONTEXT_CLASS : DEFAULT_CONTEXT_CLASS);</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">catch</span> (ClassNotFoundException ex) &#123;</span><br><span class="line">			<span class="keyword">throw</span> <span class="keyword">new</span> IllegalStateException(</span><br><span class="line">					<span class="string">"Unable create a default ApplicationContext, "</span></span><br><span class="line">							+ <span class="string">"please specify an ApplicationContextClass"</span>,</span><br><span class="line">					ex);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> (ConfigurableApplicationContext) BeanUtils.instantiate(contextClass);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<ul>
<li>Class.forName(this.webEnvironment ? DEFAULT_WEB_CONTEXT_CLASS : DEFAULT_CONTEXT_CLASS);判断当前是否是web环境并决定创建什么类；</li>
<li>如果是web程序，创建org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext的实例；</li>
<li>如果不是web程序，则创建org.springframework.context.annotation.AnnotationConfigApplicationContext的实例。</li>
</ul>
</blockquote>
<h5 id="6-new-FailureAnalyzers-context"><a href="#6-new-FailureAnalyzers-context" class="headerlink" title="6. new FailureAnalyzers(context);"></a>6. new FailureAnalyzers(context);</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Create a new &#123;<span class="doctag">@link</span> FailureAnalyzers&#125; instance.</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> context the source application context</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@since</span> 1.4.1</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="title">FailureAnalyzers</span><span class="params">(ConfigurableApplicationContext context)</span> </span>&#123;</span><br><span class="line">	<span class="keyword">this</span>(context, <span class="keyword">null</span>);</span><br><span class="line">&#125;</span><br><span class="line">FailureAnalyzers(ConfigurableApplicationContext context, ClassLoader classLoader) &#123;</span><br><span class="line">	Assert.notNull(context, <span class="string">"Context must not be null"</span>);</span><br><span class="line">	<span class="keyword">this</span>.classLoader = (classLoader == <span class="keyword">null</span> ? context.getClassLoader() : classLoader);</span><br><span class="line">	<span class="keyword">this</span>.analyzers = loadFailureAnalyzers(<span class="keyword">this</span>.classLoader);</span><br><span class="line">	prepareFailureAnalyzers(<span class="keyword">this</span>.analyzers, context);</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">private</span> List&lt;FailureAnalyzer&gt; <span class="title">loadFailureAnalyzers</span><span class="params">(ClassLoader classLoader)</span> </span>&#123;</span><br><span class="line">	List&lt;String&gt; analyzerNames = SpringFactoriesLoader</span><br><span class="line">			.loadFactoryNames(FailureAnalyzer.class, classLoader);</span><br><span class="line">	List&lt;FailureAnalyzer&gt; analyzers = <span class="keyword">new</span> ArrayList&lt;FailureAnalyzer&gt;();</span><br><span class="line">	<span class="keyword">for</span> (String analyzerName : analyzerNames) &#123;</span><br><span class="line">		<span class="keyword">try</span> &#123;</span><br><span class="line">			Constructor&lt;?&gt; constructor = ClassUtils.forName(analyzerName, classLoader)</span><br><span class="line">					.getDeclaredConstructor();</span><br><span class="line">			ReflectionUtils.makeAccessible(constructor);</span><br><span class="line">			analyzers.add((FailureAnalyzer) constructor.newInstance());</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">catch</span> (Throwable ex) &#123;</span><br><span class="line">			logger.trace(<span class="string">"Failed to load "</span> + analyzerName, ex);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	AnnotationAwareOrderComparator.sort(analyzers);</span><br><span class="line">	<span class="keyword">return</span> analyzers;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<ul>
<li>创建FailureAnalyzers对象并实例化，起构造器中调用了FailureAnalyzers &gt;&gt;&gt; loadFailureAnalyzers(this.classLoader); &gt;&gt;&gt; loadFactoryNames(FailureAnalyzer.class, classLoader);</li>
<li>步骤是从classpath下的JAR文件中包含的/META/spring.factories文件中找到org.springframework.boot.diagnostics.FailureAnalyzer对应的属性，实例化并排序，赋值给FailureAnalyzers对象的analyzers属性；</li>
<li>主要是用来处理启动时发生一些异常时的一些分析,context是否是正常加载了。</li>
</ul>
</blockquote>
<h5 id="7-prepareContext-context-environment-listeners-applicationArguments-printedBanner"><a href="#7-prepareContext-context-environment-listeners-applicationArguments-printedBanner" class="headerlink" title="7. prepareContext(context, environment, listeners, applicationArguments, printedBanner);"></a>7. prepareContext(context, environment, listeners, applicationArguments, printedBanner);</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">prepareContext</span><span class="params">(ConfigurableApplicationContext context,	ConfigurableEnvironment environment, SpringApplicationRunListeners listeners,</span></span></span><br><span class="line"><span class="function"><span class="params">		ApplicationArguments applicationArguments, Banner printedBanner)</span> </span>&#123;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//eApplicationContext 设置environment</span></span><br><span class="line">	context.setEnvironment(environment);</span><br><span class="line">	<span class="comment">//</span></span><br><span class="line">	postProcessApplicationContext(context);</span><br><span class="line">	applyInitializers(context);</span><br><span class="line">	listeners.contextPrepared(context);</span><br><span class="line">	<span class="keyword">if</span> (<span class="keyword">this</span>.logStartupInfo) &#123;</span><br><span class="line">		logStartupInfo(context.getParent() == <span class="keyword">null</span>);</span><br><span class="line">		logStartupProfileInfo(context);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">// Add boot specific singleton beans</span></span><br><span class="line">	context.getBeanFactory().registerSingleton(<span class="string">"springApplicationArguments"</span>,</span><br><span class="line">			applicationArguments);</span><br><span class="line">	<span class="keyword">if</span> (printedBanner != <span class="keyword">null</span>) &#123;</span><br><span class="line">		context.getBeanFactory().registerSingleton(<span class="string">"springBootBanner"</span>, printedBanner);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">// Load the sources</span></span><br><span class="line">	Set&lt;Object&gt; sources = getSources();</span><br><span class="line">	Assert.notEmpty(sources, <span class="string">"Sources must not be empty"</span>);</span><br><span class="line">	load(context, sources.toArray(<span class="keyword">new</span> Object[sources.size()]));</span><br><span class="line">	listeners.contextLoaded(context);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h6 id="7-1-我们是SpringBoot-web项目-所以调用此方法-AnnotationConfigEmbeddedWebApplicationContext-gt-gt-gt-setEnvironment"><a href="#7-1-我们是SpringBoot-web项目-所以调用此方法-AnnotationConfigEmbeddedWebApplicationContext-gt-gt-gt-setEnvironment" class="headerlink" title="7-1 我们是SpringBoot web项目 所以调用此方法 AnnotationConfigEmbeddedWebApplicationContext &gt;&gt;&gt; setEnvironment"></a>7-1 我们是SpringBoot web项目 所以调用此方法 AnnotationConfigEmbeddedWebApplicationContext &gt;&gt;&gt; setEnvironment</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * &#123;<span class="doctag">@inheritDoc</span>&#125;</span></span><br><span class="line"><span class="comment"> * &lt;p&gt;</span></span><br><span class="line"><span class="comment"> * Delegates given environment to underlying &#123;<span class="doctag">@link</span> AnnotatedBeanDefinitionReader&#125; and</span></span><br><span class="line"><span class="comment"> * &#123;<span class="doctag">@link</span> ClassPathBeanDefinitionScanner&#125; members.</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setEnvironment</span><span class="params">(ConfigurableEnvironment environment)</span> </span>&#123;</span><br><span class="line">	<span class="keyword">super</span>.setEnvironment(environment);</span><br><span class="line">	<span class="keyword">this</span>.reader.setEnvironment(environment);</span><br><span class="line">	<span class="keyword">this</span>.scanner.setEnvironment(environment);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h6 id="7-2-postProcessApplicationContext-context"><a href="#7-2-postProcessApplicationContext-context" class="headerlink" title="7-2 postProcessApplicationContext(context);"></a>7-2 postProcessApplicationContext(context);</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Apply any relevant post processing the &#123;<span class="doctag">@link</span> ApplicationContext&#125;. Subclasses can</span></span><br><span class="line"><span class="comment"> * apply additional processing as required.</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> context the application context</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">postProcessApplicationContext</span><span class="params">(ConfigurableApplicationContext context)</span> </span>&#123;</span><br><span class="line">	<span class="keyword">if</span> (<span class="keyword">this</span>.beanNameGenerator != <span class="keyword">null</span>) &#123;</span><br><span class="line">		context.getBeanFactory().registerSingleton(</span><br><span class="line">				AnnotationConfigUtils.CONFIGURATION_BEAN_NAME_GENERATOR,</span><br><span class="line">				<span class="keyword">this</span>.beanNameGenerator);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span> (<span class="keyword">this</span>.resourceLoader != <span class="keyword">null</span>) &#123;</span><br><span class="line">		<span class="keyword">if</span> (context <span class="keyword">instanceof</span> GenericApplicationContext) &#123;</span><br><span class="line">			((GenericApplicationContext) context)</span><br><span class="line">					.setResourceLoader(<span class="keyword">this</span>.resourceLoader);</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">if</span> (context <span class="keyword">instanceof</span> DefaultResourceLoader) &#123;</span><br><span class="line">			((DefaultResourceLoader) context)</span><br><span class="line">					.setClassLoader(<span class="keyword">this</span>.resourceLoader.getClassLoader());</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<ul>
<li>设置上下文的beanNameGenerator和resourceLoader(如果SpringApplication有的话)</li>
<li>BeanNameGenerator ： 用于通过根据给定的bean definition 生成一个bean的名字</li>
</ul>
</blockquote>
<h6 id="7-3-applyInitializers-context"><a href="#7-3-applyInitializers-context" class="headerlink" title="7-3 applyInitializers(context);"></a>7-3 applyInitializers(context);</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Apply any &#123;<span class="doctag">@link</span> ApplicationContextInitializer&#125;s to the context before it is</span></span><br><span class="line"><span class="comment"> * refreshed.</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> context the configured ApplicationContext (not refreshed yet)</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@see</span> ConfigurableApplicationContext#refresh()</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@SuppressWarnings</span>(&#123; <span class="string">"rawtypes"</span>, <span class="string">"unchecked"</span> &#125;)</span><br><span class="line"><span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">applyInitializers</span><span class="params">(ConfigurableApplicationContext context)</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">for</span> (ApplicationContextInitializer initializer : getInitializers()) &#123;</span><br><span class="line">		Class&lt;?&gt; requiredType = GenericTypeResolver.resolveTypeArgument(</span><br><span class="line">				initializer.getClass(), ApplicationContextInitializer.class);</span><br><span class="line">		Assert.isInstanceOf(requiredType, context, <span class="string">"Unable to call initializer."</span>);</span><br><span class="line">		initializer.initialize(context);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<ul>
<li>ApplicationContextInitializer initializer : getInitializers() 获取实例化SpringApplication对象的时候设置的ApplicationContextInitializer并上下文做初始化，</li>
<li>如果忘记了！妈蛋查一下 初始化SpringApplication构造器的initlalize()方法 看下里面的etInitializers((Collection) getSpringFactoriesInstances(<pre><code>ApplicationContextInitializer.class));
</code></pre></li>
<li>DelegatingApplicationContextInitializer: 从环境中(配置的context.initializer.classes)取出所有的ApplicationContextInitializer并执行</li>
<li>ContextIdApplicationContextInitializer: 设置上下文的id(name + “:” + profiles + “:” + index)，调试的id为(application:xxx:8080)</li>
</ul>
</blockquote>
<h6 id="7-4-listeners-contextPrepared-context"><a href="#7-4-listeners-contextPrepared-context" class="headerlink" title="7-4 listeners.contextPrepared(context);"></a>7-4 listeners.contextPrepared(context);</h6><p>此处其实就是个空方法 啥也没干 不知道为啥这么做！<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">contextPrepared</span><span class="params">(ConfigurableApplicationContext context)</span> </span>&#123;</span><br><span class="line">	<span class="keyword">for</span> (SpringApplicationRunListener listener : <span class="keyword">this</span>.listeners) &#123;</span><br><span class="line">		listener.contextPrepared(context);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">contextPrepared</span><span class="params">(ConfigurableApplicationContext context)</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>然后就是打印日志的代码：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (<span class="keyword">this</span>.logStartupInfo) &#123;</span><br><span class="line">	logStartupInfo(context.getParent() == <span class="keyword">null</span>);</span><br><span class="line">	logStartupProfileInfo(context);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">  .   ____          _            __ _ _</span><br><span class="line"> /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \</span><br><span class="line">( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \</span><br><span class="line"> \\/  ___)| |_)| | | | | || (_| |  ) ) ) )</span><br><span class="line">  '  |____| .__|_| |_|_| |_\__, | / / / /</span><br><span class="line"> =========|_|==============|___/=/_/_/_/</span><br><span class="line"> :: Spring Boot ::       (v1.5.11.RELEASE)</span><br><span class="line"></span><br><span class="line">2018-08-04 00:35:11.281  INFO 19488 --- [           main] com.maozw.DynamicDataSourceApplication   : Starting DynamicDataSourceApplication on maozw with PID 19488 (E:\source\spring-cloud-parent\creditnet-cloud-parent\spring-cloud-dynamicdatasource-pro\target\classes started by bjmaozhengwei in E:\source\spring-cloud-parent\creditnet-cloud-parent)</span><br><span class="line">2018-08-04 00:35:11.915  INFO 19488 --- [           main] com.maozw.DynamicDataSourceApplication   : No active profile set, falling back to default profiles: default</span><br></pre></td></tr></table></figure>
<h6 id="7-5-context-getBeanFactory-registerSingleton-“springApplicationArguments”-applicationArguments"><a href="#7-5-context-getBeanFactory-registerSingleton-“springApplicationArguments”-applicationArguments" class="headerlink" title="7-5 context.getBeanFactory().registerSingleton(“springApplicationArguments”,applicationArguments);"></a>7-5 context.getBeanFactory().registerSingleton(“springApplicationArguments”,applicationArguments);</h6><blockquote>
<ul>
<li>往上下文的beanFactory中注册一个singleton的bean,bean的名字是springApplicationArguments，bean的实例是之前实例化的ApplicationArguments对象</li>
</ul>
</blockquote>
<h6 id="7-6-接着走："><a href="#7-6-接着走：" class="headerlink" title="7-6 接着走："></a>7-6 接着走：</h6><blockquote>
<ul>
<li>如果之前获取的printedBanner不为空，那么往上下文的beanFactory中注册一个singleton的bean，bean的名字是springBootBanner，bean的实例就是这个printedBanner</li>
</ul>
</blockquote>
<h6 id="7-7-load-context-sources-toArray-new-Object-sources-size"><a href="#7-7-load-context-sources-toArray-new-Object-sources-size" class="headerlink" title="7-7 load(context, sources.toArray(new Object[sources.size()]));"></a>7-7 load(context, sources.toArray(new Object[sources.size()]));</h6><p>下面是load方法的调用链，<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Load beans into the application context.</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> context the context to load beans into</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> sources the sources to load</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">load</span><span class="params">(ApplicationContext context, Object[] sources)</span> </span>&#123;</span><br><span class="line">	<span class="keyword">if</span> (logger.isDebugEnabled()) &#123;</span><br><span class="line">		logger.debug(</span><br><span class="line">				<span class="string">"Loading source "</span> + StringUtils.arrayToCommaDelimitedString(sources));</span><br><span class="line">	&#125;</span><br><span class="line">	BeanDefinitionLoader loader = createBeanDefinitionLoader(</span><br><span class="line">			getBeanDefinitionRegistry(context), sources);</span><br><span class="line">	<span class="keyword">if</span> (<span class="keyword">this</span>.beanNameGenerator != <span class="keyword">null</span>) &#123;</span><br><span class="line">		loader.setBeanNameGenerator(<span class="keyword">this</span>.beanNameGenerator);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span> (<span class="keyword">this</span>.resourceLoader != <span class="keyword">null</span>) &#123;</span><br><span class="line">		loader.setResourceLoader(<span class="keyword">this</span>.resourceLoader);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span> (<span class="keyword">this</span>.environment != <span class="keyword">null</span>) &#123;</span><br><span class="line">		loader.setEnvironment(<span class="keyword">this</span>.environment);</span><br><span class="line">	&#125;</span><br><span class="line">	loader.load();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Load the sources into the reader.</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> the number of loaded beans</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">load</span><span class="params">()</span> </span>&#123;</span><br><span class="line">	<span class="keyword">int</span> count = <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">for</span> (Object source : <span class="keyword">this</span>.sources) &#123;</span><br><span class="line">		count += load(source);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> count;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">int</span> <span class="title">load</span><span class="params">(Object source)</span> </span>&#123;</span><br><span class="line">	Assert.notNull(source, <span class="string">"Source must not be null"</span>);</span><br><span class="line">	<span class="keyword">if</span> (source <span class="keyword">instanceof</span> Class&lt;?&gt;) &#123;</span><br><span class="line">		<span class="keyword">return</span> load((Class&lt;?&gt;) source);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span> (source <span class="keyword">instanceof</span> Resource) &#123;</span><br><span class="line">		<span class="keyword">return</span> load((Resource) source);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span> (source <span class="keyword">instanceof</span> Package) &#123;</span><br><span class="line">		<span class="keyword">return</span> load((Package) source);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span> (source <span class="keyword">instanceof</span> CharSequence) &#123;</span><br><span class="line">		<span class="keyword">return</span> load((CharSequence) source);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">throw</span> <span class="keyword">new</span> IllegalArgumentException(<span class="string">"Invalid source type "</span> + source.getClass());</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">int</span> <span class="title">load</span><span class="params">(Class&lt;?&gt; source)</span> </span>&#123;</span><br><span class="line">	<span class="keyword">if</span> (isGroovyPresent()) &#123;</span><br><span class="line">		<span class="comment">// Any GroovyLoaders added in beans&#123;&#125; DSL can contribute beans here</span></span><br><span class="line">		<span class="keyword">if</span> (GroovyBeanDefinitionSource.class.isAssignableFrom(source)) &#123;</span><br><span class="line">			GroovyBeanDefinitionSource loader = BeanUtils.instantiateClass(source,</span><br><span class="line">					GroovyBeanDefinitionSource.class);</span><br><span class="line">			load(loader);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">//  isComponent(source) &gt;&gt;&gt;&gt; AnnotationUtils.findAnnotation(type, Component.class) != null : @SpringBootApplication注 &gt;&gt;&gt; @SpringBootConfiguration注解，@SpringBootConfiguration注解 &gt;&gt;&gt; @Configuration注解，@Configuration注解又继承了@Component注解 所以返回ture</span></span><br><span class="line">	<span class="keyword">if</span> (isComponent(source)) &#123;</span><br><span class="line">		<span class="keyword">this</span>.annotatedReader.register(source);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">register</span><span class="params">(Class&lt;?&gt;... annotatedClasses)</span> </span>&#123;</span><br><span class="line">	<span class="keyword">for</span> (Class&lt;?&gt; annotatedClass : annotatedClasses) &#123;</span><br><span class="line">		registerBean(annotatedClass);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Register a bean from the given bean class, deriving its metadata from</span></span><br><span class="line"><span class="comment"> * class-declared annotations.</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> annotatedClass the class of the bean</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@SuppressWarnings</span>(<span class="string">"unchecked"</span>)</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">registerBean</span><span class="params">(Class&lt;?&gt; annotatedClass)</span> </span>&#123;</span><br><span class="line">	registerBean(annotatedClass, <span class="keyword">null</span>, (Class&lt;? extends Annotation&gt;[]) <span class="keyword">null</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<blockquote>
<ul>
<li>调用load方法注册启动类的bean定义；</li>
<li>load方法内会创建BeanDefinitionLoader的对象，并调用它的load()方法；</li>
<li>对所有的source(这里只有一个: class com.dylan.java.springboot.template.Application)都执行一次load(Object source)方法，这个方法又会调用load(Class&lt;?&gt; source)方法</li>
<li>注册该BeanDefinition</li>
</ul>
</blockquote>
<h6 id="7–8-listeners-contextLoaded-context"><a href="#7–8-listeners-contextLoaded-context" class="headerlink" title="7–8 listeners.contextLoaded(context);"></a>7–8 listeners.contextLoaded(context);</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">contextLoaded</span><span class="params">(ConfigurableApplicationContext context)</span> </span>&#123;</span><br><span class="line">	<span class="keyword">for</span> (SpringApplicationRunListener listener : <span class="keyword">this</span>.listeners) &#123;</span><br><span class="line">		listener.contextLoaded(context);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">contextLoaded</span><span class="params">(ConfigurableApplicationContext context)</span> </span>&#123;</span><br><span class="line">	<span class="keyword">for</span> (ApplicationListener&lt;?&gt; listener : <span class="keyword">this</span>.application.getListeners()) &#123;</span><br><span class="line">		<span class="keyword">if</span> (listener <span class="keyword">instanceof</span> ApplicationContextAware) &#123;</span><br><span class="line">			((ApplicationContextAware) listener).setApplicationContext(context);</span><br><span class="line">		&#125;</span><br><span class="line">		context.addApplicationListener(listener);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">this</span>.initialMulticaster.multicastEvent(</span><br><span class="line">			<span class="keyword">new</span> ApplicationPreparedEvent(<span class="keyword">this</span>.application, <span class="keyword">this</span>.args, context));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<ul>
<li>listeners的contextLoaded方法,此时上下文已经完全加载；</li>
<li>所以我们需要通知监听器，找到ApplicationListener 进行遍历；</li>
<li>（如果该listener继承了ApplicationContextAware类，那么在这一步会调用它的setApplicationContext方法，设置context）；</li>
<li>遍历完ApplicationListener之后，创建ApplicationPreparedEvent事件对象，并广播出去，也就是调用所有ApplicationListener的onApplicationEvent方法</li>
</ul>
</blockquote>
<h5 id="8-收-回到run方法-refreshContext-context"><a href="#8-收-回到run方法-refreshContext-context" class="headerlink" title="8. 收 回到run方法 refreshContext(context);"></a>8. 收 回到run方法 refreshContext(context);</h5></the>
      
    </div>
    
  </div>
  
    
    <div class="copyright">
        <p><span>本文标题:</span><a href="/maoge-blog/2018/08/05/花神(maoge)/9.Spring/SpringBoot/SpringBoot-启动源码解析/">1.SpringBoot源码分析之SpringBoot的启动过程</a></p>
        <p><span>文章作者:</span><a href="/" title="回到主页">zhengweiMao</a></p>
        <p><span>发布时间:</span>2018-08-05, 12:06:00</p>
        <p><span>最后更新:</span>2018-08-07, 21:37:32</p>
        <p>
            <span>原始链接:</span><a class="post-url" href="/maoge-blog/2018/08/05/花神(maoge)/9.Spring/SpringBoot/SpringBoot-启动源码解析/" title="1.SpringBoot源码分析之SpringBoot的启动过程">https://maozw.gitee.io/maoge-blog/2018/08/05/花神(maoge)/9.Spring/SpringBoot/SpringBoot-启动源码解析/</a>
            <span class="copy-path" data-clipboard-text="原文: https://maozw.gitee.io/maoge-blog/2018/08/05/花神(maoge)/9.Spring/SpringBoot/SpringBoot-启动源码解析/　　作者: zhengweiMao" title="点击复制文章链接"><i class="fa fa-clipboard"></i></span>
            <script> var clipboard = new Clipboard('.copy-path'); </script>
        </p>
        <p>
            <span>许可协议:</span><i class="fa fa-creative-commons"></i> <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/" title="CC BY-NC-SA 4.0 International" target = "_blank">"署名-非商用-相同方式共享 4.0"</a> 转载请保留原文链接及作者。
        </p>
    </div>



    <nav id="article-nav">
        
        
            <div id="article-nav-older" class="article-nav-title">
                <a href="/maoge-blog/2018/08/05/花神(maoge)/11.java-reflections/2.反射框架--reflections-抓取实例/">
                    1.反射框架--reflections-抓取实例
                </a>
            </div>
        
    </nav>

  
</article>

    <div id="toc" class="toc-article">
        <strong class="toc-title">文章目录</strong>
        
            <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#SpringBoot源码分析之SpringBoot的启动过程"><span class="toc-number">1.</span> <span class="toc-text">SpringBoot源码分析之SpringBoot的启动过程</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#序言"><span class="toc-number">1.0.1.</span> <span class="toc-text">序言</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#注意不同版本的可能会存在差异-我这次解析的版本是1-5-11-RELEASE"><span class="toc-number">2.</span> <span class="toc-text">注意不同版本的可能会存在差异 我这次解析的版本是1.5.11.RELEASE</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#SpringBoot的启动代码"><span class="toc-number">2.0.1.</span> <span class="toc-text">SpringBoot的启动代码</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#1-OK！-那我们分析下这段代码SpringBoot究竟帮我们做了什么事情！"><span class="toc-number">2.0.1.1.</span> <span class="toc-text">1. OK！ 那我们分析下这段代码SpringBoot究竟帮我们做了什么事情！</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#2-然后我们关注下这个构造方法具体做了什么事情呢？"><span class="toc-number">2.0.1.2.</span> <span class="toc-text">2. 然后我们关注下这个构造方法具体做了什么事情呢？</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#简要分析deduceWebEnvironment"><span class="toc-number">2.0.1.2.1.</span> <span class="toc-text">简要分析deduceWebEnvironment()</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#简要分析下deduceMainApplicationClass"><span class="toc-number">2.0.1.2.2.</span> <span class="toc-text">简要分析下deduceMainApplicationClass</span></a></li></ol></li><li class="toc-item toc-level-5"><a class="toc-link" href="#到此为止呢-我们的SpringApplication对象初始化完成了！"><span class="toc-number">2.0.1.3.</span> <span class="toc-text">到此为止呢 我们的SpringApplication对象初始化完成了！</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-分析完其构造器调用了SpringApplication-initialize-方法之后-我们在看看它的run-String…-args-方法"><span class="toc-number">2.0.2.</span> <span class="toc-text">3.分析完其构造器调用了SpringApplication.initialize()方法之后,我们在看看它的run(String… args)方法</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#3-1简述configureHeadlessProperty-方法"><span class="toc-number">2.0.2.1.</span> <span class="toc-text">3-1简述configureHeadlessProperty();方法</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#3-2简述getRunListeners-args-方法"><span class="toc-number">2.0.2.1.1.</span> <span class="toc-text">3-2简述getRunListeners(args);方法</span></a></li></ol></li><li class="toc-item toc-level-5"><a class="toc-link" href="#3-3-OK-继续回来run-方法里面-getRunListeners-args-方法之后-调用了listeners-starting"><span class="toc-number">2.0.2.2.</span> <span class="toc-text">3-3 OK 继续回来run()方法里面 getRunListeners(args);方法之后 调用了listeners.starting();</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#3-5-DefaultApplicationArguments-args"><span class="toc-number">2.0.2.3.</span> <span class="toc-text">3-5 DefaultApplicationArguments(args);</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#3-6-prepareEnvironment-listeners-applicationArguments"><span class="toc-number">2.0.2.4.</span> <span class="toc-text">3-6 prepareEnvironment(listeners, applicationArguments);</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#3-6-1-简述getOrCreateEnvironment-方法"><span class="toc-number">2.0.2.4.1.</span> <span class="toc-text">3-6-1 简述getOrCreateEnvironment();方法</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#3-6-2-configureEnvironment-…-方法"><span class="toc-number">2.0.2.4.2.</span> <span class="toc-text">3-6-2 configureEnvironment(…);方法</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#3-6-3-listeners-environmentPrepared-environment-方法"><span class="toc-number">2.0.2.4.3.</span> <span class="toc-text">3-6-3 listeners.environmentPrepared(environment);方法</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#3-6-3-1-我们看下onApplicationEvent方法"><span class="toc-number">2.0.2.4.4.</span> <span class="toc-text">3-6-3-1 我们看下onApplicationEvent方法</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#3-6-3-2看下getSearchLocations-方法；"><span class="toc-number">2.0.2.4.5.</span> <span class="toc-text">3-6-3-2看下getSearchLocations()方法；</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#3-6-3-3-load-具体加载文件的方法"><span class="toc-number">2.0.2.4.6.</span> <span class="toc-text">3-6-3-3 load() 具体加载文件的方法</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#3-6-3-4-紧接着loadIntoGroup-又调用doLoadIntoGroup方法，这个方法会检查配置文件是否存在-该方法就不再贴出来了-找到后进行解析，"><span class="toc-number">2.0.2.4.7.</span> <span class="toc-text">3-6-3-4 紧接着loadIntoGroup() : 又调用doLoadIntoGroup方法，这个方法会检查配置文件是否存在,该方法就不再贴出来了; 找到后进行解析，</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#3-6-3-5-this-propertiesLoader-load-resource-group-name-profile-null-null-profile-getName"><span class="toc-number">2.0.2.4.8.</span> <span class="toc-text">3-6-3-5 this.propertiesLoader.load(resource, group, name,(profile == null ? null : profile.getName()));</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#3-6-3-6-解析完成：开始调用handleProfileProperties-propertySource"><span class="toc-number">2.0.2.4.9.</span> <span class="toc-text">3-6-3-6 解析完成：开始调用handleProfileProperties(propertySource);</span></a></li></ol></li><li class="toc-item toc-level-5"><a class="toc-link" href="#4-至此结束后，我们回到run-方法中。"><span class="toc-number">2.0.2.5.</span> <span class="toc-text">4. 至此结束后，我们回到run()方法中。</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#4-1-Banner-printedBanner-printBanner-environment"><span class="toc-number">2.0.2.6.</span> <span class="toc-text">4-1 Banner printedBanner = printBanner(environment);</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#5-createAndRefreshContext-方法-创建容器的方法-该方法创建SpringBoot的上下文"><span class="toc-number">2.0.2.7.</span> <span class="toc-text">5. createAndRefreshContext()方法 创建容器的方法 该方法创建SpringBoot的上下文.</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#6-new-FailureAnalyzers-context"><span class="toc-number">2.0.2.8.</span> <span class="toc-text">6. new FailureAnalyzers(context);</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#7-prepareContext-context-environment-listeners-applicationArguments-printedBanner"><span class="toc-number">2.0.2.9.</span> <span class="toc-text">7. prepareContext(context, environment, listeners, applicationArguments, printedBanner);</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#7-1-我们是SpringBoot-web项目-所以调用此方法-AnnotationConfigEmbeddedWebApplicationContext-gt-gt-gt-setEnvironment"><span class="toc-number">2.0.2.9.1.</span> <span class="toc-text">7-1 我们是SpringBoot web项目 所以调用此方法 AnnotationConfigEmbeddedWebApplicationContext &gt;&gt;&gt; setEnvironment</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#7-2-postProcessApplicationContext-context"><span class="toc-number">2.0.2.9.2.</span> <span class="toc-text">7-2 postProcessApplicationContext(context);</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#7-3-applyInitializers-context"><span class="toc-number">2.0.2.9.3.</span> <span class="toc-text">7-3 applyInitializers(context);</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#7-4-listeners-contextPrepared-context"><span class="toc-number">2.0.2.9.4.</span> <span class="toc-text">7-4 listeners.contextPrepared(context);</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#7-5-context-getBeanFactory-registerSingleton-“springApplicationArguments”-applicationArguments"><span class="toc-number">2.0.2.9.5.</span> <span class="toc-text">7-5 context.getBeanFactory().registerSingleton(“springApplicationArguments”,applicationArguments);</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#7-6-接着走："><span class="toc-number">2.0.2.9.6.</span> <span class="toc-text">7-6 接着走：</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#7-7-load-context-sources-toArray-new-Object-sources-size"><span class="toc-number">2.0.2.9.7.</span> <span class="toc-text">7-7 load(context, sources.toArray(new Object[sources.size()]));</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#7–8-listeners-contextLoaded-context"><span class="toc-number">2.0.2.9.8.</span> <span class="toc-text">7–8 listeners.contextLoaded(context);</span></a></li></ol></li><li class="toc-item toc-level-5"><a class="toc-link" href="#8-收-回到run方法-refreshContext-context"><span class="toc-number">2.0.2.10.</span> <span class="toc-text">8. 收 回到run方法 refreshContext(context);</span></a></li></ol></li></ol></li></ol></li></ol>
        
    </div>
    <style>
        .left-col .switch-btn,
        .left-col .switch-area {
            display: none;
        }
        .toc-level-3 i,
        .toc-level-3 ol {
            display: none !important;
        }
    </style>

    <input type="button" id="tocButton" value="隐藏目录"  title="点击按钮隐藏或者显示文章目录">

    <script>
        yiliaConfig.toc = ["隐藏目录", "显示目录", !!"false"];
    </script>



    
<div class="share">
    
        <div class="bdsharebuttonbox">
            <a href="#" class="fa fa-twitter bds_twi" data-cmd="twi" title="分享到推特"></a>
            <a href="#" class="fa fa-weibo bds_tsina" data-cmd="tsina" title="分享到新浪微博"></a>
            <a href="#" class="fa fa-qq bds_sqq" data-cmd="sqq" title="分享给 QQ 好友"></a>
            <a href="#" class="fa fa-files-o bds_copy" data-cmd="copy" title="复制网址"></a>
            <a href="#" class="fa fa fa-envelope-o bds_mail" data-cmd="mail" title="通过邮件分享"></a>
            <a href="#" class="fa fa-weixin bds_weixin" data-cmd="weixin" title="生成文章二维码"></a>
            <a href="#" class="fa fa-share-alt bds_more" data-cmd="more"></i></a>
        </div>
        <script>
            window._bd_share_config={
                "common":{"bdSnsKey":{},"bdText":"1.SpringBoot源码分析之SpringBoot的启动过程　| 花神　","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"24"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];
        </script>
    

    
</div>







    




    <div class="scroll" id="post-nav-button">
        
            <a href="/" title="回到主页"><i class="fa fa-home"></i></a>
        

        <a title="文章列表"><i class="fa fa-bars"></i><i class="fa fa-times"></i></a>

        
            <a href="/maoge-blog/2018/08/05/花神(maoge)/11.java-reflections/2.反射框架--reflections-抓取实例/" title="下一篇: 1.反射框架--reflections-抓取实例">
                <i class="fa fa-angle-right"></i>
            </a>
        
    </div>

    <ul class="post-list"><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/08/05/花神(maoge)/9.Spring/SpringBoot/SpringBoot-启动源码解析/">1.SpringBoot源码分析之SpringBoot的启动过程</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/08/05/花神(maoge)/11.java-reflections/2.反射框架--reflections-抓取实例/">1.反射框架--reflections-抓取实例</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/08/05/花神(maoge)/11.服务器相关/1.服务器--Sigar 获取CPU和Memory内存等信息使用详解/">1.Sigar 获取CPU和Memory内存等信息使用详解</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/08/05/花神(maoge)/11.java-reflections/1.反射框架--reflections-基本介绍/">1.反射框架--reflections-基本介绍</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/08/05/花神(maoge)/10.算法/算法----外部排序/">2.算法 -外部排序</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/08/05/花神(maoge)/10.算法/算法----snowflake/">1.算法 -snowflake</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/08/05/花神(maoge)/8.面试/面试/">1.面试</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/08/01/花神(maoge)/6.quartz--定时任务框架/7.Quartz-Job状态与并发/">7.任务调度框架Quartz-Job状态与并发</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/08/01/花神(maoge)/6.quartz--定时任务框架/6.任务调度框架Quartz-config/">5.任务调度框架Quartz-Quartz-Job Stores</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/08/01/花神(maoge)/6.quartz--定时任务框架/5.任务调度框架Quartz-Job Stores/">5.任务调度框架Quartz-Quartz-Job Stores</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/08/01/花神(maoge)/6.quartz--定时任务框架/4.任务调度框架Quartz-SchedulerListeners/">4.任务调度框架Quartz-SchedulerListeners</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/08/01/花神(maoge)/6.quartz--定时任务框架/3.任务调度框架Quartz-TriggerListeners和JobListeners/">3.任务调度框架Quartz-Triggers-SimpleTrigger</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/08/01/花神(maoge)/6.quartz--定时任务框架/2-3.任务调度框架Quartz-Triggers-CronTrigger/">2-2.任务调度框架Quartz-Triggers-CronTrigger</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/08/01/花神(maoge)/6.quartz--定时任务框架/2-2.任务调度框架Quartz-Triggers-SimpleTrigger/">2-2.任务调度框架Quartz-Triggers-SimpleTrigger</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/08/01/花神(maoge)/6.quartz--定时任务框架/2-1.任务调度框架Quartz-Triggers详解理解/">2-1.任务调度框Quartz-Triggers详解理解</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/08/01/花神(maoge)/6.quartz--定时任务框架/1.任务调度框架Quartz-入门/">1.任务调度框架Quartz-入门</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/12/花神(maoge)/9.Spring/SpringMVC--源码解读/05.SpringMVC-----源码分析与实践--HandlerMapping(1)/">1.SpringMVC -源码分析与实践-HandlerMapping</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/12/花神(maoge)/9.Spring/SpringMVC--源码解读/04.SpringMVC-----源码分析与实践--DispatcherServlet核心/">4.SpringMVC -源码分析与实践-DispatcherServlet</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/12/花神(maoge)/9.Spring/SpringMVC--源码解读/03.SpringMVC-----源码分析与实践--初始化分析/">3.SpringMVC -初始化源码分析</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/12/花神(maoge)/9.Spring/SpringMVC--源码解读/02.SpringMVC-----源码分析与实践--环境搭建/">2.SpringMVC -源码分析与实践--环境搭建</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/12/花神(maoge)/7.JAVA_WEB相关技术/1.基于AsyncRestTemplate异步HTTP请求的一种轻量级技术实现/">1.基于AsyncRestTemplate异步HTTP请求的一种轻量级技术实</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/12/花神(maoge)/9.Spring/SpringMVC--源码解读/01.SpringMVC-----源码分析与实践----初步认知/">1.SpringMVC -源码分析与实践----初步认知</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/20.并发编程--并发框架-Disruptor--应用示例3/">20.并发编程--并发框架-Disruptor--应用示例3</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/19.并发编程--并发框架-Disruptor--应用示例2/">19并发编程--并发框架-Disruptor--应用示例2</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/18.并发编程--并发框架-Disruptor--应用示例1/">18.并发编程--并发框架-Disruptor--应用示例1</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/17.并发编程--并发框架-Disruptor--术语介绍/">17.并发编程--并发框架-Disruptor--介绍</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/16.并发编程--并发框架-Disruptor--入门介绍/">16.并发编程--并发框架-Disruptor--介绍</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/15.并发编程--锁-介绍/">15.并发编程--锁-介绍</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/14.并发编程--Concurrent-工具类/">14.并发编程--Concurrent-工具类介绍</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/13.并发编程--线程池介绍/">13. 并发编程--线程池</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/12.并发编程--多线程设计模式 - 生产者-消费者模式/">12并发编程\12.并发编程--多线程设计模式 - 生产者-消费者模式</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/11.并发编程--多线程设计模式 - Master-Worker模式/">11多线程设计模式 - Master-Worker模式</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/7.并发编程--单例模式(饥饿模式-懒汉模式)/">7.并发编程--单例模式(饥饿模式-懒汉模式)</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/10.并发编程--多线程设计模式 - Future模式/">10 多线程设计模式 - Future模式</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/8.并发编程--同步类容器和并发类容器/">8并发编程--同步类容器和并发类容器</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/6.并发编程--ThreadLocal/">6.并发编程--ThreadLocal</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/9.并发编程--Queue/">9并发编程--Queue</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/5.并发编程--多线程通信-wait-notify-模拟Queue/">并发编程--多线程通信-wait-notify-模拟Queue</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/4-2.并发编程--多线程通信-wait-notify-notifyAll-join/">4-2.并发编程--并发编程--多线程通信-wait-notify-notifyAll-join</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/4-1.并发编程--多线程通信-wait-notify/">4.并发编程--多线程通信-wait-notify</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/1.并发编程--线程基础/">并发编程--线程基础</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/3.并发编程--volatile 细节说明/">并发编程--volatile</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/2.并发编程-synchronized 细节说明/">并发编程-synchronized 细节说明</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/0.并发编程-readMe/">并发编程</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/05/05/花神(maoge)/2.网络编程/0.并发编程-readMe/">网络编程</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/05/05/花神(maoge)/2.网络编程/13.网络编程--Netty-文件的上传和下载/">13.网络编程--Netty-文件的上传和下载</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/05/05/花神(maoge)/2.网络编程/14.网络编程--mina-入门/">14.网络编程--mina-入门</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/05/05/花神(maoge)/2.网络编程/12.网络编程--Netty-实践-心跳检测/">12.网络编程--Netty-实践-心跳检测</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/05/05/花神(maoge)/2.网络编程/11.网络编程--Netty-实践-数据通信/">11.网络编程--Netty-实践-数据通信+心跳检测</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/05/05/花神(maoge)/2.网络编程/10.网络编程--Netty-基础-WebSocket/">10.网络编程--Netty-基础-WebSocket</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/05/05/花神(maoge)/2.网络编程/9.网络编程--Netty-基础-UDP/">9.网络编程--Netty-UDP</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/05/05/花神(maoge)/2.网络编程/8.网络编程--Netty-基础-拆包粘包-2/">8.网络编程--Netty-拆包粘包</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/05/05/花神(maoge)/2.网络编程/7.网络编程--Netty-基础-拆包粘包-1/">7.网络编程--Netty-拆包粘包</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/05/05/花神(maoge)/2.网络编程/6.网络编程--Netty-基础-HelloWorld/">6.网络编程--Netty-HelloWorld</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/05/05/花神(maoge)/2.网络编程/5.网络编程--Netty-基础-入门/">5.网络编程--Netty-入门</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/05/05/花神(maoge)/2.网络编程/4.网络编程--基础-AIO异步实现/">4.网络编程--基础-AIO异步实现</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/05/05/花神(maoge)/2.网络编程/3.网络编程--基础-NIO异步实现/">3.网络编程--基础-NIO异步实现</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/05/05/花神(maoge)/2.网络编程/2.网络编程--基础-BIO伪异步实现/">网络编程--基础</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/05/05/花神(maoge)/2.网络编程/1.网络编程--基础-socket/">网络编程--基础</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/04/05/花神(maoge)/5.分布式--zookeeper/2.zookeeper--集群搭建/">2.分布式--zookeeper\2.zookeeper--集群搭建</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/04/05/花神(maoge)/5.分布式--zookeeper/1.zookeeper--简介/">1.分布式--zookeeper--zookeeper简介</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/02/05/花神(maoge)/4.JVM相关/1.jvm虚拟机组成部分/">1.jvm虚拟机组成部分</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/02/05/花神(maoge)/3.缓存-中间件/Redis 配置文件说明/">Redis集群搭建-配置文件</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/02/05/花神(maoge)/3.缓存-中间件/Redis----集群搭建/">Redis集群搭建-实践</a></li></ul>




    <script>
        
    </script>
</div>
      <footer id="footer">
    <div class="outer">
        <div id="footer-info">
            <div class="footer-left">
                <i class="fa fa-copyright"></i> 
                2016-2018 zhengweiMao
            </div>
            <div class="footer-right">
                <a href="http://hexo.io/" target="_blank" title="快速、简洁且高效的博客框架">Hexo</a>  Theme <a href="https://github.com/MOxFIVE/hexo-theme-yelee" target="_blank" title="简而不减 Hexo 双栏博客主题  v3.5">Yelee</a> by MOxFIVE <i class="fa fa-heart animated infinite pulse"></i>
            </div>
        </div>
        
            <div class="visit">
                
                    <span id="busuanzi_container_site_pv" style='display:none'>
                        <span id="site-visit" title="本站到访数"><i class="fa fa-user" aria-hidden="true"></i><span id="busuanzi_value_site_uv"></span>
                        </span>
                    </span>
                
                
                    <span>| </span>
                
                
                    <span id="busuanzi_container_page_pv" style='display:none'>
                        <span id="page-visit"  title="本页阅读量"><i class="fa fa-eye animated infinite pulse" aria-hidden="true"></i><span id="busuanzi_value_page_pv"></span>
                        </span>
                    </span>
                
            </div>
        
    </div>
</footer>
    </div>
    
<script data-main="/maoge-blog/js/main.js" src="//cdn.bootcss.com/require.js/2.2.0/require.min.js"></script>

    <script>
        $(document).ready(function() {
            var iPad = window.navigator.userAgent.indexOf('iPad');
            if (iPad > -1 || $(".left-col").css("display") === "none") {
                var bgColorList = ["#9db3f4", "#414141", "#e5a859", "#f5dfc6", "#c084a0", "#847e72", "#cd8390", "#996731"];
                var bgColor = Math.ceil(Math.random() * (bgColorList.length - 1));
                $("body").css({"background-color": bgColorList[bgColor], "background-size": "cover"});
            }
            else {
                var backgroundnum = 5;
                var backgroundimg = "url(/background/bg-x.jpg)".replace(/x/gi, Math.ceil(Math.random() * backgroundnum));
                $("body").css({"background": backgroundimg, "background-attachment": "fixed", "background-size": "cover"});
            }
        })
    </script>





<div class="scroll" id="scroll">
    <a href="#" title="返回顶部"><i class="fa fa-arrow-up"></i></a>
    <a href="#comments" onclick="load$hide();" title="查看评论"><i class="fa fa-comments-o"></i></a>
    <a href="#footer" title="转到底部"><i class="fa fa-arrow-down"></i></a>
</div>
<script>
    // Open in New Window
    
        var oOpenInNew = {
            
            
            
            
            
            
             archives: ".archive-article-title", 
             miniArchives: "a.post-list-link", 
            
             friends: "#js-friends a", 
             socail: ".social a" 
        }
        for (var x in oOpenInNew) {
            $(oOpenInNew[x]).attr("target", "_blank");
        }
    
</script>

<script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js">
</script>
  </div>
</body>
</html>